|
-
May 10th, 2012, 01:07 PM
#1
Thread Starter
New Member
Paint application | Multiple pen strokes with different colors
I am new to VB. I am trying to make a 'paint' type application, where you can choose a pen color, draw lines, and if you choose to change the color, the original lines will remain unchanged and you can continue your drawing with a different color.
Below is the code that I have so far. What happens is when you start to draw, and then change the pen color, every line that has been created will change color. I want to make it so that choosing a new color will not effect the previous lines that have been drawn.
I tried to make mousePath a dynamic array to maybe store each line as an additional object, but I kept getting exceptions.
Please advise...
Public Class Form1
Public myColor As Color
Dim mousePath As New System.Drawing.Drawing2D.GraphicsPath
Dim i As Integer
Public Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = MouseButtons.Left Then
mousePath.StartFigure()
End If
End Sub
Public Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If e.Button = MouseButtons.Left Then
Try
mousePath.AddLine(e.X, e.Y, e.X, e.Y)
Catch
End Try
End If
PictureBox1.Invalidate()
End Sub
Public Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
If i = 0 Then
myColor = Color.Black
i = i + 1
End If
Dim CurrentPen = New Pen(myColor, 2)
Try
e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
e.Graphics.DrawPath(CurrentPen, mousePath)
Catch ex As Exception
End Try
End Sub
Private Sub ShowColo*******tripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ShowColo*******tripMenuItem.Click
If i > 0 Then
If (ColorDialog1.ShowDialog() = DialogResult.OK) Then
myColor = ColorDialog1.Color
i = i + 1
End If
End If
End Sub
End Class
-
May 11th, 2012, 04:56 PM
#2
Re: Paint application | Multiple pen strokes with different colors
Hi npopovic, welcome to VB forums.
You are making one bad mistake in your code and you do it several times. You should never use Try...Catch with nothing in the Catch clause. It's just a way of fooling yourself. So get rid of the Try, Catch and End try statements. They are not helping you.
You'll discover there is something wrong in the MouseMove sub. You are adding a line to a GraphicsPath but you are using the same values for the starting point and the ending point (e.X, e.Y). That's a line of zero length, which isn't allowed -- but at present you are ignoring it.
The correct way to draw a line with the mouse is this:
1. declare a couple of integer variables -- call them lastX and lastY for example -- at Form level.
2. in the MouseDown sub, set lastX and lastY equal to e.X and e.Y respectively.
3. in the MouseMove sub, add a line to the path from LastX, LastY to e.X,e.Y.
4. then, still in the MouseMove sub, set LastX and LastY to the values of e.X and e.Y.
I haven't checked the rest of your code properly, but at first sight it looks like it should work if you make the above changes.
BB
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|