How to clear graphics on top of picture box?

machinogodzilla

Active member
Joined
Mar 3, 2007
Messages
25
Programming Experience
Beginner
Hi!


I ve created a picture box, loaded some bitmap into it and drew on top of it
with:

PictureBox.CreateGraphics.FillPolygon(System.Drawing.Brushes.Red, Points)

Now I would like to get rid of this polygon to draw a different one.

This is what I tried bt did not help much:

PicutreBox.Image = Nothing ; clears everything including the bitmap underneath.

PictureBox.CreateGraphics.Clear(Color.Transparent) ; gets rid of all drawings but also fill the picture box with black colour (why?).

PictureBox.refresh() ; this works fine except the image flickers when there are many changes in short period of time.

What else can I do?


TIA for any help!

Regards, machinogodzilla
 
You really shouldn't use CreateGraphics except in rare cases. Use the controls Paint method instead. In the paint method you could use the GDI+ DrawImage method to draw the image then use the FillPolygon method similar to above but instead use the PaintEventArgs.Graphics property in place of PictureBox.CreateGraphics.
As an alternative you can set the image property of the PictureBox in combination with the Paint Event.
 
There are at least two relatively easy ways to do it...

First, the picturebox has two "layers" that you can draw on. You can assign your bitmap to the Picturebox.BackgroundImage and then do your drawing on the PictureBox.Image. In this case when you clear the PictureBox.Image, the .BackgroundImage would remain.

The second method would allow an unlimited number of layers for you to draw on. You can create a bitmap array which is just like layers in a paint program. You draw on the various layers and then you have a draw routine that collapses all the layers into one image that is displayed on the PictureBox.Image. In this case, I always use layer 0 to store my background image.

Personally, I don't use CreateGraphics, or the PaintEventArgs.Graphics... I prefer to draw on bitmaps, or bitmap arrays, in memory and then when I'm ready to update the display in the picturebox I just set the PictureBox.Image equal to the appropriate bitmap layer.
 
Last edited:
Back
Top