Fare hareketi ile görüntü üzerinde birkaç çizgi çizin

0

Soru

Bir görüntü üzerinde birkaç çizgi çizmek istiyorum Fareyi kullanarak içeri taşı vb.net. Benim sorunum, bir sonraki satırı çizmeye başladığımda, önceki satırın kaybolmasıdır! Herkes bana yardımcı olabilir? Kodumu projemi çalıştıran bir fotoğrafla birlikte koydum

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

GIF of my run project

drawing vb.net
2021-11-23 06:07:57
1

En iyi cevabı

0

Satırları tamamlandıkça bir listede saklayabilir, ardından canlı olanı çizerken önceki tüm satırları çizebilirsiniz

Private st, en As New Point
Private mouseButtonPressed As Boolean = False

Private lines As New List(Of (st As Point, en As Point))

Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
    Dim p = New Pen(Color.Black, 2)
    Dim g As Graphics = e.Graphics
    g.DrawLine(p, st, en)
    For Each line In lines
        g.DrawLine(p, line.st, line.en)
    Next
End Sub

Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
    mouseButtonPressed = False
    lines.Add((st, en))
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

Liste geçerli Tuple(of Point, Point)

2021-11-23 15:48:51

Diğer dillerde

Bu sayfa diğer dillerde

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................