繁体   English   中英

如何在VB.Net PictureBox上动态创建叠加层

[英]How to dynamically create an overlay over a VB.Net PictureBox

我在窗体form1上有一个VB.Net PictureBox floorPlanImage

我将图片加载到图片框中:

    floorPlanImage.image = my.resources.ResourceManager.GetObject("level8") 'this is actually dynamic, and this part works

我正在尝试创建一个叠加层以突出显示图像的某个区域:

    Public Sub highlightPrintArea(ByVal x1 As Integer, ByVal y1 As Integer, ByVal x2 As Integer, ByVal y2 As Integer)
    '**** DOES NOT WORK
    Dim g As Graphics = Me.CreateGraphics
    Dim r As Rectangle = New Rectangle(x1, y1, x2 - x1, y2 - y1) 'these are args passed in to the function
    Dim pen As Pen = New Pen(Color.FromArgb(128, 32, 100, 200), 1) 'semi-transparent
    Dim b As Brush = New SolidBrush(pen.Color)

    g.FillRectangle(b, r)
    end sub

我需要在运行时动态地执行此操作,例如,在按钮单击时。 上面的函数似乎没有绘制矩形。

但是,如果我有一个Handles floorPlanImage.Paint函数,如下所示,那么矩形就像我期望的那样绘制:

Private Sub floorPlanImage_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles floorPlanImage.Paint
    '**** Works, but does not suit my workflow
    Dim g As Graphics = e.Graphics
    Dim r As Rectangle = New Rectangle(100, 100, 100, 100)
    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 Sub

问题(最后)

如何修改我的onclick函数以正确覆盖我的PictureBox上的矩形?

在onclick事件中,您需要将位置/点保存到成员变量并设置标志,以便应用知道您已保存位置。 要更新图片框,请调用Invalidate and Update。

floorPlanImage.Invalidate()
floorPlanImage.Update()

在onpaint事件测试中,您有一个点的标志然后使用保存的点来绘制叠加层。

Private Sub floorPlanImage_Paint(ByVal sender As Object, ByVal e As  System.Windows.Forms.PaintEventArgs) Handles floorPlanImage.Paint
    If hasPoint

       'Draw with saved point
    End If
End Sub

暂无
暂无

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

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