Help with advanced paint application

daveofgv

Well-known member
Joined
Sep 17, 2008
Messages
218
Location
Dallas, TX
Programming Experience
1-3
Would it be possible if someone can help with my paint application?

I need an eraser tool for the user and some other cool effects.

What I have now is:

VB.NET:
Public Class doodletime
  
    Dim xStart, yStart, xEnd, yEnd As Integer
    Dim Drawbitmap As Bitmap
    Dim Drawgraphics As Graphics
    Dim myPen As New Pen(Color.BlueViolet, 3)
    Dim myColor As Color = Color.Blue
    Dim myBrush As New Drawing.SolidBrush(Color.Red)
    Dim myBrushWidth As Integer
    Dim ContinuousFlag As Boolean
    Private Sub drawMyline()
        PictureBox1.Image = Drawbitmap
        Drawgraphics.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
        Drawgraphics.DrawLine(myPen, xStart, yStart, xEnd, yEnd)
    End Sub



    Private Sub doodletime_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Drawbitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Drawgraphics = Graphics.FromImage(Drawbitmap)
        PictureBox1.Image = Drawbitmap
        Drawgraphics.Clear(Color.White)
        myBrushWidth = 4
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click

    End Sub

    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
        xStart = Control.MousePosition.X - (Me.Left + PictureBox1.Left + 4)
        yStart = Control.MousePosition.Y - (Me.Top + PictureBox1.Top + 31)
        'to do continuous drawing, enable this line
        'drawMyline()
        If RadioButton1.Checked = True Then
            ContinuousFlag = True
        End If
    End Sub

    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
        If ContinuousFlag Then
            Drawgraphics.SmoothingMode = Drawing2D.SmoothingMode.None
            Drawgraphics.FillEllipse(myBrush, e.X, e.Y, myBrushWidth, myBrushWidth)
            PictureBox1.Image = Drawbitmap
        End If
    End Sub

    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
        xEnd = Control.MousePosition.X - (Me.Left + PictureBox1.Left + 4)
        yEnd = Control.MousePosition.Y - (Me.Top + PictureBox1.Top + 31)
        If RadioButton1.Checked Then
            ContinuousFlag = False
        Else
            drawMyline()

        End If
    End Sub

    Private Sub Button14_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button14.Click
        ColorDialog1.ShowDialog()
        myPen.Color = ColorDialog1.Color
        myBrush.Color = ColorDialog1.Color
    End Sub
 
    Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
        Dim Savefiledialog1 As New SaveFileDialog()
        Savefiledialog1.Filter = "Bitmap Image (*.bmp)|*.bmp |All Files |*.*"
        If Savefiledialog1.ShowDialog() = DialogResult.OK Then
            PictureBox1.Image.Save(Savefiledialog1.FileName, System.Drawing.Imaging.ImageFormat.Bmp)

        End If
    End Sub

    Private Sub ButtonX2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX2.Click
        Drawbitmap = New Bitmap(PictureBox1.Width, PictureBox1.Height)
        Drawgraphics = Graphics.FromImage(Drawbitmap)
        PictureBox1.Image = Drawbitmap
        Drawgraphics.Clear(Color.White)
    End Sub

    Private Sub ButtonX3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX3.Click
        Me.Close()
    End Sub

    Private Sub Button15_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button15.Click
        With OpenFileDialog1
            '.InitialDirectory = "C:\"
            .Filter = "All Files|*.*|Bitmaps|*.bmp|GIFs|*.gif|JPEGs|*.jpg"

        End With
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            PictureBox1.Image = Image.FromFile(OpenFileDialog1.FileName)
            PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
            PictureBox1.BorderStyle = BorderStyle.Fixed3D
            lblFilePath.Text = OpenFileDialog1.FileName()

        End If
    End Sub

    Private Sub PanelEx1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PanelEx1.Click

    End Sub
End Class

I have tried several methods of an eraser, but nothing really works good. Also, I am needing help with adding a picture (which I can) and being able to draw on the picture (which I can not).

Also, If I should be using a panel instead of using a picturebox can someone show me how? I tried and it says panel1.image is not a memeber of the system.panel......
If anyone can help I would sure appreciate it.

Thanks in advanced.

daveofgv
 
Last edited:
I am not sure what you mean. Do you mean make lines an store them so you can remove them later? I find it better to make a line class object that I store in a Generics List this way i can add/remove as needed. The line class will store the points for you. Plus you have to have a list of things that are drawn in the paint event else they disappear next paint. As far as using a panel, I would say I have had better luck with a PictureBox when I an refreshing often, and as you found you cannot add an image to a property, but you could paint the image. Also I hope you are disposing the Drawgraphics object - better yet just pass the e.Graphics object to that sub when painting.
 
For freehand (continuous) eraser, since your backcolor is White:
VB.NET:
myBrush.Color = Color.White
 
Thanks for the replies.

I am trying to make it, also, where I can upload an image and draw on top of that image. So if I were to have a picture of a car and "doodled" over the car (and messed up) I would like to erase just that one part where I messed up instead of clearing the whole thing. Kinda like Microsofts Paint Brush with the eraser tool........

Does that make sense?

Hope this is a little more detailed of what I would like to find out?

Daveofgv
 
You can load an Image into a PictureBox and the use GDI+ to draw on the PictureBox as much as you like. When you're ready to save, you use the same code to draw on the Image itself. You simply pass a Graphics object created from the Image instead of the one from the PictureBox's Paint event hander. Here's a simple example:

Very Simple Drawing Program
 
Back
Top