0% found this document useful (0 votes)
122 views2 pages

Graphics - How To Dynamically Create An Overlay Over A VB - Net PictureBox - Stack Overflow

Uploaded by

Chad Dole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
122 views2 pages

Graphics - How To Dynamically Create An Overlay Over A VB - Net PictureBox - Stack Overflow

Uploaded by

Chad Dole
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

OverflowAI is here! AI power for your Stack Overflow for Teams knowledge community.

Learn more

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


Asked 13 years, 2 months ago Modified 13 years, 2 months ago Viewed 5k times

I have a VB.Net PictureBox floorPlanImage on a form form1 .

I load a picture into the picturebox:


1
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:

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?

vb.net graphics paint picturebox


Share Improve this question Follow edited Mar 17, 2011 at 22:41 asked Mar 17, 2011 at 6:11
Antony
3,781 1 26 32

1 Answer Sorted by: Highest score (default)

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. To update the picture box call Invalidate and Update.
4
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.

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

Share Improve this answer Follow answered Mar 17, 2011 at 22:59
Ben Martin
770 4 7

1 The only thing I would add would be to use the Invalidate overload that accepts a rectangle to keep from repainting the
entire control. – Mark Hall Mar 17, 2011 at 23:06

Thanks @Ben and @Mark. I don't do a lot of this - thanks for the direction. – Antony Mar 17, 2011 at 23:21

You might also like