Draw Line and Rectangle on PictureBox using Mouse

horia

New member
Joined
May 24, 2016
Messages
1
Programming Experience
1-3
Hello!

I have an project created by vb.Net 2010.The ptoject has a PictureBox and I draw Grid in to PictureBox .Using Mouse to draw Polygons.
I want to draw Line and Rectangle only where the Line Crosses(x,y-axis).
Please help me!

Thank you!
[XCODE]
[XCODE]
[/XCODE][/XCODE]

  • Private Sub picTest_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picTest.MouseDown
  • Dim hstep As Double = picTest.Width / 12
  • Dim vstep As Double = picTest.Height / 12
  • Dim myPen As New Pen(Color.Red, 3)
  • Dim g As Graphics = Graphics.FromImage(gBitmap)
  • picAG.Refresh()
  • If e.Button = System.Windows.Forms.MouseButtons.Left Then
  • points.Add(e.Location) ' add point on left click
  • For i As Integer = 0 To points.Count - 1
  • g.DrawRectangle(myPen, points(i).X - 2, points(i).Y - 2, 5, 5)
  • Next
  • End If
  • If (NewPolygon IsNot Nothing) Then
  • If (e.Button = MouseButtons.Right) Then
  • gBitmap = Nothing
  • ' Finish this polygon.
  • If (NewPolygon.Count > 0) Then Polygons.Add(NewPolygon)
  • NewPolygon = Nothing
  • picTest.Image = gBitmap
  • Else
  • ' Add a point to this polygon.
  • If (NewPolygon(NewPolygon.Count - 1) <> e.Location) Then
  • Dim i As Integer
  • For i = 0 To 12
  • NewPolygon.Add(New Point(e.X, e.Y )'Here I think to use'hstep,vstep and i',but I don`t know how to do ?!
  • picTest.Image = gBitmap
  • Next i
  • End If
  • End If
  • Else
  • ' Start a new polygon.
  • NewPolygon = New List(Of Point)()
  • NewPolygon.Add(e.Location)
  • NewPoint = (e.Location)
  • End If
  • ' Redraw.
  • picTest.Invalidate()
  • 'Dispose of objects
  • myPen.Dispose()
  • g.Dispose()
  • Private Sub picTest_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picTest.MouseMove
  • If (NewPolygon Is Nothing) Then Exit Sub
  • NewPoint = e.Location
  • ' Redraw.
  • picTest.Invalidate()
  • End Sub
  • Private Sub picTest_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picTest.Paint
  • Dim g As Graphics = e.Graphics
  • Dim myPen As New Pen(Color.Red, 2)
  • If (NewPolygon IsNot Nothing) Then
  • ' Draw the new polygon.
  • If (NewPolygon.Count > 1) Then
  • e.Graphics.DrawLines(myPen, NewPolygon.ToArray())
  • For i As Integer = 0 To points.Count - 1
  • e.Graphics.FillEllipse(Brushes.Green, points(i).X - 2, points(i).Y - 2, 5, 5)
  • Next
  • End If
  • End If
  • Dim hstep As Double = picTest.Width / 12
  • Dim vstep As Double = picTest.Height / 12
  • 'Draw horizontal Line
  • Dim X As Single = hstep
  • For i As Integer = 0 To hstep
  • g.DrawLine(New Pen(Color.Black, 2), New Point(X, 0), New Point(X, Height))
  • X += hstep
  • Next i
  • Dim Y As Single = vstep
  • For i As Integer = 0 To vstep
  • 'Draw Vertical Line
  • g.DrawLine(New Pen(Color.Black, 2), New Point(0, Y), New Point(Width, Y))
  • Y += vstep
  • Next i
  • End Sub
 

Latest posts

Back
Top