【问题标题】:How to dynamically create an overlay over a VB.Net PictureBox如何在 VB.Net PictureBox 上动态创建覆盖
【发布时间】:2011-03-18 06:59:23
【问题描述】:

我在表单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 上?

【问题讨论】:

    标签: vb.net graphics paint picturebox


    【解决方案1】:

    在 onclick 事件中,您需要将位置/点保存到成员变量并设置一个标志,以便应用知道您保存了一个位置。要更新图片框,请调用 Invalidate 和 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
    

    【讨论】:

    • 我唯一要添加的是使用接受矩形的 Invalidate 重载来防止重新绘制整个控件。
    • 感谢@Ben 和@Mark。我做的不多 - 感谢您的指导。
    猜你喜欢
    • 2013-02-04
    • 2017-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多