NullifyNetwork

The blog and home page of Simon Soanes
Skip to content
[ Log On ]

Ever wondered how to double buffer with a Graphics object so your GDI+ based game/control doesn't flicker annoyingly?

Me too.  There's probably a built in method that's easier, but this is how I managed to get it to work smoothly, it's nice and simple and allows you to draw anywhere that offers up the normal CreateGraphics method.

First, set up a bitmap to act as your backbuffer:

Bitmap BackBuffer = new Bitmap(this.ClientSize.Width,this.ClientSize.Height);

Graphics DrawingArea = Graphics.FromImage(BackBuffer);

Next, you want to draw to your graphics object as normal, so DrawingArea.Clear(Color.Black); and such.

Once you've completed drawing the object that you want to smoothly move, simply draw the pre-rendered bitmap over the top of the Graphics object you want to update:

Graphics Viewable = this.CreateGraphics();

Viewable.DrawImageUnscaled(BackBuffer, 0, 0);

You can also use other techniques to increase the performance, such as reusing the backbuffer by defining it in the class you're using it in - this means .NET won't need to recreate it repeatedly.

Permalink