Click here to Skip to main content
Click here to Skip to main content

Flicker free drawing using GDI+ and C#

, 28 Jul 2003 Public Domain
Rate this:
Please Sign up or sign in to vote.
Describes how to implement flicker free drawing using C# and GDI+.
<!-- Article image -->

Sample Image - flickerFreeDrawing.jpg

<!-- Add the rest of your HTML here -->


This article describes how to implement flicker free drawing on Windows Forms using GDI+, it assumes you have a basic understanding or VS.NET, C# and the .NET framework.


Flicker free drawing or double buffering is a well know technique used in the Windows programming world to reduce flicker when handling paint events in a window.

Normally a generic window programs draw directly to the device context (Graphics Object) when a WM_PAINT (Paint) event occurs. This can lead to flickering if the window is refreshed (Invalidated) repeatedly. Three examples where flickering happen would be during a Window resize or animation (a timer is fired and in the timer event the window is refreshed) or when a object is dragged over the window (e.g. Visio)

We can eliminate flickering using a technique known as double buffering. Rather than drawing directly on the graphics object, we draw to an off screen graphics object and when the drawing is complete we draw the off screen graphics object onto the graphics object supplied by the Paint event. We also override the OnPaintBackground method to prevent the windows form performing any background rendering (we must paint the background ourselves during the rendering in the off screen graphics object, this is usually the first thing that is done).

The double buffering technique is encapsulated in a simple class called DBGraphics and can be easily implemented in a typical windows form based application show below.

Using the code

The double buffering class can be used within the scope of the windows form. The steps below describe how to implement the DBGraphics class in your code:

  • Step 1 - Declare the DBGraphics variable in your windows form class and instantiate the object in the windows form constructor.
using GDIDB; // Declare the namespace
 public class MainWnd : System.Windows.Forms.Form
     ... Some other code
    private DBGraphics memGraphics;
     ... Some other code
    public MainWnd()
        memGraphics = new  DBGraphics();
  • Step 2 - Handle the resize and load event to create the double buffer object to the size of the Client Rectangle. This is done in form load event as the resize event only gets fire when the form is manually resized. One thing to note here is we need to obtain the graphics object of the form even though we are not in the Paint event, this done by calling this.CreateGraphics()is is similar to GetDC().
private void MainWnd_Load(object sender, System.EventArgs e)
    memGraphics.CreateDoubleBuffer(this.CreateGraphics(), this.ClientRectangle.Width, this.ClientRectangle.Height);
private void MainWnd_Resize(object sender, System.EventArgs e)
    memGraphics.CreateDoubleBuffer(this.CreateGraphics(), this.ClientRectangle.Width, this.ClientRectangle.Height);
    Invalidate(); // Force a repaint after has been resized 

  • Step 3 - Override the OnPaintBackground is to allow the paint event to render the background.
protected override void OnPaintBackground(PaintEventArgs pevent)
  • Step 4 - Finally implement the Paint event
protected override void Paint(object sender, System.Windows.Forms.PaintEventArgs e)

    if (memGraphics.CanDoubleBuffer())
    // Fill in Background (for effieciency only the area that has been clipped)
         memGraphics.g.FillRectangle(new SolidBrush(SystemColors.Window), e.ClipRectangle.X,e.ClipRectangle.Y, e.ClipRectangle.Width, e.ClipRectangle.Height);

        // Do our drawing using memGraphics.g instead e.Graphics
        ... Some other code
       // Render to the form

Demonstration Code

The demonstration code show how to implement simple drag and drop interface can achieved using double buffering, it can be used a springbroad for a drag and drop application such as Microsoft Visio.


V1.0 Article creation.


This article, along with any associated source code and files, is licensed under A Public Domain dedication


About the Author

Software Developer (Senior) Software Kinetics
United Kingdom United Kingdom

Software Kinetics
are experts in developing customised and bespoke applications and have expertise in the development of desktop, mobile and internet applications on Windows.

We specialise in:

  • User Interface Design
  • Desktop Development
  • Windows Phone Development
  • Windows Presentation Framework
  • Windows Forms
  • Windows Communication Framework
  • Windows Services
  • Network Applications
  • Database Applications
  • Web Development
  • Web Services
  • Silverlight
Visit Software Kinetics
Follow on   Twitter

Comments and Discussions

Generalit is cool thank you . Pinmemberkankankan2226-Dec-12 4:51 
QuestionDoubleBuffer [modified] Pinmemberjanvb20-Nov-11 5:00 
GeneralThank you very much PinmemberJoyetta9-Oct-11 4:46 
GeneralMy results PinmemberKaiwa7-Apr-10 3:36 
GeneralRe: My results PingroupNorm .net7-Apr-10 3:45 
GeneralRe: My results PinmemberKaiwa7-Apr-10 7:58 
GeneralGood not perfect but good PinmemberCAD Zombie20-Oct-09 6:09 
GeneralDrawing an image without flicker Pinmemberpcbaby1516-Jul-09 16:41 
Generalthis.DoubleBuffered Pinmemberdfsdfsdfoyuti28-Feb-09 5:37 
GeneralRe: this.DoubleBuffered PinmemberNorm .net1-Mar-09 21:45 
QuestionCan i use this DBGraphics class for drwaing the controls on my form Pinmemberbhaskar soni5-Jan-09 2:27 
QuestionWhat license is the code under? Pinmemberbinary12301-Dec-08 22:28 
AnswerRe: What license is the code under? PinmemberNorm .net2-Dec-08 5:48 
GeneralRe: What license is the code under? Pinmemberbinary12302-Dec-08 19:30 
GeneralNice job! Pinmembersandeepstudd15-Jul-08 2:06 
Jokethank u very much Pinmembersilent200713-Jul-08 23:08 
GeneralThanks PinmemberPALANI KUMAR13-Mar-08 0:45 
GeneralCotrolStyle.DoubleBuffer Pinmembervadivelkumar5-Jul-07 22:26 
QuestionBackgound Objects? PinmemberDoncp7-Dec-06 7:43 
AnswerRe: Backgound Objects? Pinmemberzhaoxiong198629-Aug-10 18:32 
GeneralThanks! Pinmemberminity30-Jul-06 3:51 
GeneralDrawing.... Pinmemberzmrcic13-Mar-06 4:06 
Generalif ((width != this.width) || (height != this.height)) PinmemberMarijn Stevens12-Mar-06 13:06 
GeneralRe: if ((width != this.width) || (height != this.height)) Pinmembersupercali4-Apr-06 15:51 
GeneralStill having issues PinmemberPonsonby24-Jan-06 0:15 
GeneralNice Job! Pinmemberjinzhecheng30-Nov-05 5:31 
GeneralFlickers worse than before, I must be missing something Pinmembersklett2-Oct-05 8:23 
GeneralRe: Flickers worse than before, I must be missing something Pinmemberjaypea23-Nov-05 4:48 
GeneralRe: Flickers worse than before, I must be missing something Pinmemberjaypea28-Nov-05 6:23 
GeneralRe: Flickers worse than before, I must be missing something Pinmemberjaypea28-Nov-05 6:24 
GeneralRe: Flickers worse than before, I must be missing something PinmemberLPlateDeveloper8-Oct-08 0:15 
GeneralRe: Flickers worse than before, I must be missing something Pinmemberdxwnd6-Jan-10 19:42 
GeneralThanks Pinmemberidda19-Sep-05 23:19 
Generaladded reset method Pinmembertogil28-Jul-05 2:14 
GeneralRe: added reset method Pinmembernorm.net28-Jul-05 2:24 
GeneralRe: added reset method Pinmemberjmanson19-Dec-05 18:03 
GeneralRe: added reset method Pinmembernorm.net19-Dec-05 21:36 
GeneralOn minimize event.. Pinmemberday13t1-Mar-05 23:44 
GeneralRe: On minimize event.. Pinmemberpphc13-Jun-05 17:05 
GeneralRe: On minimize event.. Pinmembernorm.net19-Sep-05 23:36 
GeneralRe: On minimize event.. PinmemberChrisWard31-May-07 2:19 
GeneralRe: On minimize event.. Pinmembernorm .net31-May-07 2:24 
GeneralVery good piece of code much stronger then double buffering Pinmemberasherab2-Jan-05 22:40 
GeneralA better method (?) PinmemberTeisDraiby15-Oct-04 16:35 
GeneralOops! PinmemberTeisDraiby15-Oct-04 16:44 
GeneralRe: A better method (!!!) PinmemberBenoit Nesme27-Jan-06 1:48 
QuestionCan this be done in VB.NET?? PinmemberRichiLloyd28-Sep-04 17:16 
AnswerRe: Can this be done in VB.NET?? PinmemberRichiLloyd28-Sep-04 17:18 
Questionbuggy built in double buffering? Pinmembersir_mad5-May-04 6:12 
Generalflicker free transparent window settings PinsussJeffrey W. Thompson16-Apr-04 10:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.1411023.1 | Last Updated 29 Jul 2003
Article Copyright 2003 by NormDroid
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid