draw a few lines on an image with mouse move

user20 161 Reputation points
2021-11-23T06:30:08.633+00:00

I want to draw a few lines on an image Using mouse move in vb.net. My problem is that when I start drawing the next line, the previous line disappears! Can anyone help me? I put my code along with a photo of run my project.

 ![Dim st, en As New Point  
    Dim p As Pen  
    Private mouseButtonPressed As Boolean = False  
  
    Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint  
        p = New Pen(Color.Black, 2)  
        Dim g As Graphics = e.Graphics  
        g.DrawLine(p, st, en)  
    End Sub  
  
    Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp  
        mouseButtonPressed = False  
    End Sub  
  
    Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove  
        If mouseButtonPressed Then  
            PictureBox1.Invalidate()  
            en = e.Location  
        End If  
    End Sub  
  
    Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown  
        If e.Button = MouseButtons.Left Then  
            st = New Point(e.X, e.Y)  
            mouseButtonPressed = True  
        End If  
    End Sub][1]  
Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,686 Reputation points
    2021-11-23T08:22:23.06+00:00

    You can use the method from Let the user draw lines in C#

    Public Class Form1        
    
        Private Segments As List(Of Segment) = New List(Of Segment)()
        Private NewSegment As Segment = Nothing
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
            NewSegment = New Segment(Pens.Blue, e.Location, e.Location)
            PictureBox1.Refresh()
        End Sub
    
        Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
            e.Graphics.Clear(PictureBox1.BackColor)
            e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    
            For Each segment As Segment In Segments
                segment.Draw(e.Graphics)
            Next
    
            If NewSegment IsNot Nothing Then NewSegment.Draw(e.Graphics)
        End Sub
    
        Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
            If NewSegment Is Nothing Then Return
            NewSegment.pt2 = e.Location
            PictureBox1.Refresh()
        End Sub
    
        Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
            If NewSegment Is Nothing Then Return
            NewSegment.pen1 = Pens.Black
            Segments.Add(NewSegment)
            NewSegment = Nothing
            PictureBox1.Refresh()
        End Sub
    End Class
    
    Class Segment
        Public pen1 As Pen
        Public pt1, pt2 As Point
    
        Public Sub New(pen As Pen, point1 As Point, point2 As Point)
            pen1 = pen
            pt1 = point1
            pt2 = point2
        End Sub
    
        Public Sub Draw(gr As Graphics)
            gr.DrawLine(pen1, pt1, pt2)
        End Sub
    End Class
    

1 additional answer

Sort by: Most helpful
  1. user20 161 Reputation points
    2021-11-23T13:36:20.603+00:00

    NOW HOW TO SAVE IMAGE with the drawn lines?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.