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]
Sure :)
Private
Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMoveDim myPen As New System.Drawing.Pen(System.Drawing.Color.Red)Dim MyGraphics As System.Drawing.GraphicsMyGraphics =
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 :)
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.GraphicsMyGraphics =
Me.PictureBox1.CreateGraphics()MyGraphics.DrawLine(myPen, StartPoint.X, StartPoint.Y, PictureBox1.PointToClient(MousePosition).X, PictureBox1.PointToClient(MousePosition).Y)
myPen.Dispose()
MyGraphics.Dispose()
end sub