将圆圈放在图片框中的鼠标位置

时间:2014-02-14 12:02:15

标签: vb.net picturebox

我做了很多搜索,找不到我需要的帮助。

我需要能够在图片框中的某些图像部分放置一些直径为140像素的圆圈。在图像上单击鼠标时,应显示圆圈。我需要在点击鼠标时将圆心放置在鼠标的位置。一旦将圆圈放在图像上,用户还应该能够将圆圈拖动到不同的位置。

有人可以就如何做到这一点给我一些指导吗?

提前致谢!

3 个答案:

答案 0 :(得分:0)

这可能会让你失望。在PictureBox.Click

上绘制一个简单的红色圆圈
Private Sub PictureBox1_Click(sender As System.Object, e As MouseEventArgs) Handles PictureBox1.Click

    Dim myBrush As New System.Drawing.SolidBrush(System.Drawing.Color.Red)
    Dim formGraphics As System.Drawing.Graphics
    formGraphics = sender.CreateGraphics()
    formGraphics.FillEllipse(myBrush, New Rectangle(e.X / 2, e.Y / 2, 70 , 70))
    myBrush.Dispose()
    formGraphics.Dispose()
End Sub

修改 看起来很像您正在寻找的东西 http://www.dreamincode.net/forums/topic/59049-simple-drawing-selection-shape-or-rubberband-shape/

答案 1 :(得分:0)

我的解决方案:`

Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    'start creating circle when the mouse is clicked 

    PictureBox1.Refresh() 'erases previous rectangle
    Xstart = e.X
    Ystart = e.Y


    bRB = True
End Sub

Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    'drag the circle over the seclected area

    If bRB Then
        PictureBox1.Refresh() 'erases previous rectangle

        Select Case e.X
            Case Is < 0
                RBRectangle.X = 0
                ' RBRectangle.Width = Xstart
                RBRectangle.Width = 140

            Case 0 To Xstart
                RBRectangle.X = e.X
                'RBRectangle.Width = Xstart - e.X
                RBRectangle.Width = 140

            Case Xstart To PBWidth
                RBRectangle.X = Xstart
                'RBRectangle.Width = e.X - Xstart
                RBRectangle.Width = 140
            Case Is > PBWidth
                RBRectangle.X = Xstart
                'RBRectangle.Width = PBWidth - Xstart
                RBRectangle.Width = 140
        End Select

        Select Case e.Y
            Case Is < 0
                RBRectangle.Y = 0
                'RBRectangle.Height = Ystart
                RBRectangle.Height = 140

            Case 0 To Ystart
                RBRectangle.Y = e.Y
                'RBRectangle.Height = Ystart - e.Y
                RBRectangle.Height = 140
            Case Ystart To PBHeight
                RBRectangle.Y = Ystart
                'RBRectangle.Height = e.Y - Ystart
                RBRectangle.Height = 140
            Case Is > PBHeight
                RBRectangle.Y = Ystart
                'RBRectangle.Height = PBHeight - Ystart
                RBRectangle.Height = 140
        End Select

        PictureBox1.CreateGraphics.DrawEllipse(RBPen, RBRectangle)
    End If
End Sub

 Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp 
    'show the area in the rectangle as a new image 
    Dim CroppedBm As Bitmap
    If bRB Then
        bRB = False



        CroppedBm = New Bitmap(RBRectangle.Width, _
        RBRectangle.Height)



    End If
End Sub

答案 2 :(得分:0)

试试这个:

Dim g As Graphics
Dim md As Boolean
Private Sub Form1_Load() Handles MyBase.Load
    g = PictureBox1.CreateGraphics
End Sub

Private Sub PictureBox1_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseClick
    PictureBox1.Refresh()
    g.DrawEllipse(Pens.Red, New Rectangle(e.X - 35, e.Y - 35, 70, 70))
End Sub

Private Sub PictureBox1_MouseDown(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseDown
    md = True
End Sub

Private Sub PictureBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseMove
    If md Then
        PictureBox1.Refresh()
        g.DrawEllipse(Pens.Red, New Rectangle(e.X - 35, e.Y - 35, 70, 70))
    End If
End Sub

Private Sub PictureBox1_MouseUp(sender As Object, e As MouseEventArgs) Handles PictureBox1.MouseUp
    md = False
End Sub