tags:

views:

49

answers:

3

So yeah,

Basically I drew something on a form using Graphics. Now if I move a window over the form the stuff I drew gets erased.

Is there a way to preserve the drawing?

(I'm using Windows.Forms)

+1  A: 

You need to draw in the Paint event of a control.

Darin Dimitrov
+1  A: 

you have to repaint what you drawn in the Paint event handler.

Benny
is there another way?
Nick Brooks
easy way is drawing in a memory graphic, and in the paint event handler, copy the image from the memory graphic to the form's graphic
Benny
"is there another way?" - fundamentally no. Windows Forms is not a retained-mode graphics system: it needs to be redrawn every time, though as Benny says you can stash the graphic somewhere to minimise the actual redraw effort. But if you want a retained-mode system, where you declare or create graphical "objects" in a context, and the system takes care of painting them when required, you'd need to consider something like WPF.
itowlson
+1  A: 

Don't draw to the Form from outside the Paint event handler (which is what it sounds like you may be doing).

You can either redraw the contents every time in your Paint event handler, or you can paint once to an offscreen bitmap and then just display the bitmap in your Paint handler.

The code for both is almost identical, but to draw to an offscreen bitmap you need to set up a new bitmap for the Graphics context to draw into.

Here's an example which illustrates everything you need to do to use an offscreen bitmap to store the image. (And indeed, if you remove the offscreen-bitmap code from this example, the remaining code is how to draw in the Paint handler)

Jason Williams