Draw a square on a picturebox?

jimday1982

Member
Joined
Jul 30, 2003
Messages
22
Location
Va Beach
My program currently imports pictures into my program, but I am trying to allow the user to be able to draw a yellow, semi-transparent square on the image, to hi-lite it. I have a toolbar with a toggle button for drawing the square and thats where I stand at this point. Can anyone point me in the right direction? Thank you.
 
The best way would be to draw in the paint event, then you can use GDI+ to draw the square using FillRectangle with a color set to yellow with alpha blending.
For example: (in paint event)
Code:
e.Graphics.FillRectangle(New SolidBrush(Color.FromArgb(100, 0, 255, 255)), 100, 100, 100, 100)
draw a rectangle using solid brush with alphablending
Also, do you want the user to be able to draw more than one rectangle?
 
Last edited by a moderator:
That brings up a good point. The user should have the ability to draw multiple squares on the picture. I should also mention that this code is going into a case statement inside the click event for a toolbar. My main concern is that the user should be able to draw the square on the picture box by clicking and dragging the mouse. Any other ideas or insight is GREATLY appreciated.
 
Sorry, I mistyped my last sentence in my previous post :) (edited now).
If you want the user to be able to draw multiple boxes then this how I would suggest doing it.
Create an array list which will store Rectangle objects, then everytime a rectangle is drawn add its coordinates and size in a form of Rectangle object to the list of rectangles. Then in the Paint event of the picture box simply go through every rectangle in the array list and draw it, like this:
Code:
Dim rect As Rectangle
For each rect in RectList RectList in this example is the array list holding all rectangles
e.Graphics.FillRectangle(ColorYouWant, rect) get the rect and draw it
Next
 
Last edited by a moderator:
Ok thanks! However, there are 2 errors (probably my fault) that Im wondering if you can help me with. First, I get an underline under "e.graphics" and it says that "graphics" is not a member of system.windows.forms.toolbarbuttonclickevenargs. The second is that the directcast operand must have a reference type, but system.drawing.rectangle is a value type. Any ideas? Thanks.
 
This goes in the paint event of your picturebox you want to draw onto, not in the click event. I corrected the typo in the code so it should work now.
 
Back
Top