【问题标题】:Draw on a picturebox image using mouse vb.net使用鼠标在图片框图像上绘制 vb.net
【发布时间】:2013-02-07 23:38:54
【问题描述】:

如何在 vb.net 中使用鼠标在图片框图像上绘制线条或画笔?

【问题讨论】:

    标签: .net vb.net mouse draw picturebox


    【解决方案1】:

    a similar question 从 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
    

    【讨论】:

    • 你是神,我会为你创造一个宗教,兄弟
    • 如何使曲线更平滑?使用此代码,当我快速移动鼠标光标时,它会绘制直线
    • @anandhu:请提出一个单独的问题。如果您愿意,可以参考这篇文章。
    【解决方案2】:

    我已修改您的代码以放置一个“点”,可以在鼠标单击时移动(覆盖)

    我需要的是用于多个“点”,比如四个。

    用户从列表中选择一个标记,并将其放在图片框上,然后他们选择另一个并放置在不同的位置,但是它会覆盖之前的标记。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-03
      • 1970-01-01
      • 1970-01-01
      • 2015-11-08
      • 1970-01-01
      相关资源
      最近更新 更多