使用鼠标在图片框图像上绘制 vb.net
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13327311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Draw on a picturebox image using mouse vb.net
提问by hades
How can i draw on a picturebox image using mouse in vb.net a line or brush?
如何在 vb.net 中使用鼠标在图片框图像上绘制线条或画笔?
回答by Neolisk
Converted a similar questionfrom C# to VB.NET, using a line - tested and working:
将类似的问题从 C#转换为 VB.NET,使用一行 - 测试和工作:
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
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
Private Sub pictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
_Previous = Nothing
End Sub
回答by Martin
I have modified your code to place a "dot", which can be moved (overwritten) on mouse click
我已经修改了您的代码以放置一个“点”,可以在鼠标单击时移动(覆盖)
What I need is this to be used for more than one "dot", say four.
我需要的是将其用于多个“点”,例如四个。
The user selects a marker from the list, and places it on the picture box, they then select another and place in a different location, however it overwrites the previous marker.
用户从列表中选择一个标记,并将其放置在图片框上,然后他们选择另一个并放置在不同的位置,但它会覆盖前一个标记。
Marker = Lst_Markers.SelectedIndex + 1
If _Previous IsNot Nothing Then
For i As Integer = 0 To Marker
Dim bmp As New Bitmap(PictureBox1.Width, PictureBox1.Height)
PictureBox1.Image = bmp
Next
Select Case Marker
Case 1
'PictureBox1.Image = bmp1
Using g As Graphics = Graphics.FromImage(PictureBox1.Image)
g.FillEllipse(Brushes.Red, e.X, e.Y, 10, 10)
End Using
Case 2
'PictureBox1.Image = bmp2
Using g As Graphics = Graphics.FromImage(PictureBox1.Image)
g.FillEllipse(Brushes.Yellow, e.X, e.Y, 10, 10)
End Using
Case 3
'PictureBox1.Image = bmp3
Using g As Graphics = Graphics.FromImage(PictureBox1.Image)
g.FillEllipse(Brushes.Green, e.X, e.Y, 10, 10)
End Using
Case 4
'PictureBox1.Image = bmp4
Using g As Graphics = Graphics.FromImage(PictureBox1.Image)
g.FillEllipse(Brushes.Blue, e.X, e.Y, 10, 10)
End Using
Case Else
MsgBox("Select a marker")
End Select
PictureBox1.Invalidate()
_Previous = e.Location
End If

