Drawaing a line on an Image

Hi guys, hope you can help me out on this one :)

I'm developing a small application that contains a distance measurement tool. The idea is to make it a bit like map24.com (you clik once to select a starting point, and again to select an ending point). This works fine and I can use the graphics.drawline method to draw the line. But I would like the user to see the line already after the first click (like drawing in paint). Then when you move the mouse the ending point of the line moves. If I try to do this it draws a new line every time I move the mouse because I'm creating a new instanes og the graphics on mousemove. I have trying to make one instance and use the clear method before I draw a new line, but this method requires a color and my picture disapears... Do anyone have an idea how to make this right? (hope you can understand what I'm saying, my english isn't the best :)


[919 byte] By [RonniM] at [2007-12-24]
# 1
It may help us help you ....if you post the code that you currently have
DMan1 at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Sure :)

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove

Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)

Dim MyGraphics As System.Drawing.Graphics

MyGraphics = Me.PictureBox1.CreateGraphics()

MyGraphics.DrawLine(myPen, StartPoint.X, StartPoint.Y, PictureBox1.PointToClient(MousePosition).X, PictureBox1.PointToClient(MousePosition).Y)

myPen.Dispose()

MyGraphics.Dispose()

end sub

I have tried moving the declaritions to the top of the class so I only get one instance of the graphic object. Then I tried to call the MyGraphics.clear(transparent) method in the mousemove method, but it doesn't do the trick. I could really use that old vb6 linecontrol right now :)

RonniM at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

HI, I found a solution. It's not pretty, but it works. But if anyone has a better solution, I'm all ear :)

I'm calling the refresh method on the picturebox object before I'm creating the object. That way I make sure that all lines disappear...

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove

Me.PictureBox1.Refresh()

Dim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)

Dim MyGraphics As System.Drawing.Graphics

MyGraphics = Me.PictureBox1.CreateGraphics()

MyGraphics.DrawLine(myPen, StartPoint.X, StartPoint.Y, PictureBox1.PointToClient(MousePosition).X, PictureBox1.PointToClient(MousePosition).Y)

myPen.Dispose()

MyGraphics.Dispose()

end sub

RonniM at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
I'm not sure if it is any better than yours:

Public Class Form1
Private mStart, mEnd As Point
Private mDrawing As Boolean
Private Sub Form1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
mDrawing = Not mDrawing
If mDrawing Then mStart = e.Location
mEnd = e.Location
Me.Invalidate()
End Sub
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
If mDrawing Then
mEnd = e.Location
Me.Invalidate()
End If
End Sub
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
e.Graphics.DrawLine(Pens.Black, mStart.X, mStart.Y, mEnd.X, mEnd.Y)
End Sub
End Class

nobugz at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
It is a very bad idea to call CreateGraphics. If the control refreshes during your mouse move, your line will be gone. It is better to do as nobugz has pointed out and handle the paint event of the control. This way, when the picturbox refreshes, your line will be drawn again. Also, calling CreateGraphics only affects the pixels on the screen, not the pixels in the image you may have loaded into the picturebox control.
JoeSmugeresky at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6

Thank you. I'll try to change my solution so it's more like nobugz.

RonniM at 2007-8-31 > top of Msdn Tech,Visual Basic,Visual Basic General...