2011-03-17 54 views
1

我有一個表格form1上的VB.Net圖片框floorPlanImage如何動態創建覆蓋VB.Net圖片框

我加載圖片到PictureBox:

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 

我需要爲此在運行時動態,說,在按鈕點擊。上面的函數似乎沒有繪製矩形。

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 

問題(最終)

如何修改我:

不過,如果我有Handles floorPlanImage.Paint喜歡下面的函數,那麼矩形作爲我期望它繪製onclick函數正確覆蓋我的PictureBox上的矩形?

回答

4

在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 
+1

我要添加的唯一事情就是使用Invalidate重載,它接受矩形以防止重繪整個控件。 – 2011-03-17 23:06:31

+0

感謝@Ben和@Mark。我沒有做太多的工作 - 感謝這個方向。 – Antony 2011-03-17 23:21:28