2020年5月6日 星期三

vb.net 在PictureBox畫線 [Draw Line in PictureBox]


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
Private _Previous As System.Nullable(Of Point) = Nothing

 

Private Sub pictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown

  _Previous = e.Location

  pictureBox1_MouseMove(sender, e)

End Sub



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
Private Sub pictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove

  If _Previous IsNot Nothing Then

    If PictureBox1.Image Is Nothing Then

      Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)

      Using g As Graphics = Graphics.FromImage(bmp)

        g.Clear(Color.White)

      End Using

      PictureBox1.Image = bmp

    End If

    Using g As Graphics = Graphics.FromImage(PictureBox1.Image)

      g.DrawLine(Pens.Black, _Previous.Value, e.Location)

    End Using

    PictureBox1.Invalidate()

    _Previous = e.Location

  End If

End Sub

1
2
3
4
5
Private Sub pictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp

  _Previous = Nothing

End Sub

沒有留言:

張貼留言