[英]How to dynamically create an overlay over a VB.Net PictureBox
I have a VB.Net PictureBox floorPlanImage
on a form form1
. 我在窗体form1
上有一个VB.Net PictureBox floorPlanImage
。
I load a picture into the picturebox: 我将图片加载到图片框中:
floorPlanImage.image = my.resources.ResourceManager.GetObject("level8") 'this is actually dynamic, and this part works
I am trying to create an overlay to highlight a region of the image: 我正在尝试创建一个叠加层以突出显示图像的某个区域:
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
I need to do this dynamically at runtime, say, on button click. 我需要在运行时动态地执行此操作,例如,在按钮单击时。 The above function does not seem to draw the rectangle. 上面的函数似乎没有绘制矩形。
However, if I have a function that Handles floorPlanImage.Paint
like follows, then the rectangle is drawn as I expect it to: 但是,如果我有一个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
The Question (finally) 问题(最后)
How can I modify my onclick function to correctly overlay the rectangle over my PictureBox? 如何修改我的onclick函数以正确覆盖我的PictureBox上的矩形?
In the onclick event you need to save the location/point to a member variable and set a flag so app knows you have a location saved. 在onclick事件中,您需要将位置/点保存到成员变量并设置标志,以便应用知道您已保存位置。 To update the picture box call Invalidate and Update. 要更新图片框,请调用Invalidate and Update。
floorPlanImage.Invalidate()
floorPlanImage.Update()
In the onpaint event test the flag that you have a point then use the saved point to draw the overlay. 在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.