简体   繁体   English

Picturebox覆盖vb.net

[英]Picturebox overlay vb.net

I have written the following code but there is an issue which I am not trying to figure out.. I am trying to draw a simple line on the image in my picture box on mouse click. 我已经编写了以下代码,但是有一个问题我不想解决。.我试图在单击鼠标时在我的图片框中的图像上绘制一条简单的线。 however, paint method is called but doesnt draw it, instead the line at that point is drawn when I scroll the picture in the picture box.. Pls help 但是,会调用paint方法但不会绘制它,而是在我在图片框中滚动图片时绘制了该点的线。.请帮助

Private Sub picCurrentimage_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles picCurrentimage.MouseDown
     px = e.X
     py = e.Y
     highlightPic(px, py)
End Sub

Private Sub highlightPic(ByVal x1 As Integer, ByVal y1 As Integer)
        haspoint = True
        picCurrentimage.Invalidate()
        picCurrentimage.Update()
End Sub


Private Sub picCurrentimage_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles picCurrentimage.Paint
        If haspoint Then
            Dim g As Graphics = picCurrentimage.CreateGraphics
            Dim r As Rectangle = New Rectangle(px, py, 600, 10) 
            Dim pen As Pen = New Pen(Color.FromArgb(128, 32, 100, 200), 1) 
            Dim b As Brush = New SolidBrush(pen.Color)
            g.FillRectangle(b, r)
        End If
End Sub

Try to replace your code with the following modifications: 尝试用以下修改替换您的代码:

Private Sub highlightPic(ByVal x1 As Integer, ByVal y1 As Integer)
    haspoint = True
    picCurrentimage.Refresh()  'this do both of your lines in one
End Sub

Private Sub picCurrentimage_Paint(ByVal sender As Object, _
                                  ByVal e As PaintEventArgs) _
                                  Handles picCurrentimage.Paint
    If haspoint Then

        Dim g As Graphics = e.Graphics
        Dim r As Rectangle = picCurrentimage.ClientRectangle

        Dim b As Brush = New SolidBrush(Color.FromArgb(128, 32, 100, 200))
        g.FillRectangle(b, r)
        b.Dispose

    End If

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM