Click here to Skip to main content
13,150,178 members (29,976 online)
Click here to Skip to main content
Add your own
alternative version


150 bookmarked
Posted 28 Jul 2003

Flicker free drawing using GDI+ and C#

, 28 Jul 2003
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.
[__b__]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

You may also be interested in...


Comments and Discussions

Generalit is cool thank you . Pin
kankankan2226-Dec-12 3:51
memberkankankan2226-Dec-12 3:51 
QuestionDoubleBuffer Pin
janvb20-Nov-11 4:00
memberjanvb20-Nov-11 4:00 
GeneralThank you very much Pin
Joyetta9-Oct-11 3:46
memberJoyetta9-Oct-11 3:46 
GeneralMy results Pin
Kaiwa7-Apr-10 2:36
memberKaiwa7-Apr-10 2:36 
GeneralRe: My results Pin
Norm .net7-Apr-10 2:45
groupNorm .net7-Apr-10 2:45 
GeneralRe: My results Pin
Kaiwa7-Apr-10 6:58
memberKaiwa7-Apr-10 6:58 
GeneralGood not perfect but good Pin
CAD Zombie20-Oct-09 5:09
memberCAD Zombie20-Oct-09 5:09 
GeneralDrawing an image without flicker Pin
pcbaby1516-Jul-09 15:41
memberpcbaby1516-Jul-09 15:41 
Generalthis.DoubleBuffered Pin
dfsdfsdfoyuti28-Feb-09 4:37
memberdfsdfsdfoyuti28-Feb-09 4:37 
GeneralRe: this.DoubleBuffered Pin
Norm .net1-Mar-09 20:45
memberNorm .net1-Mar-09 20:45 
QuestionCan i use this DBGraphics class for drwaing the controls on my form Pin
bhaskar soni5-Jan-09 1:27
memberbhaskar soni5-Jan-09 1:27 
QuestionWhat license is the code under? Pin
binary12301-Dec-08 21:28
memberbinary12301-Dec-08 21:28 
AnswerRe: What license is the code under? Pin
Norm .net2-Dec-08 4:48
memberNorm .net2-Dec-08 4:48 
GeneralRe: What license is the code under? Pin
binary12302-Dec-08 18:30
memberbinary12302-Dec-08 18:30 
GeneralThis is a godsend Pin
walentys2-Aug-08 22:45
memberwalentys2-Aug-08 22:45 
GeneralNice job! Pin
sandeepstudd15-Jul-08 1:06
membersandeepstudd15-Jul-08 1:06 
Jokethank u very much Pin
silent200713-Jul-08 22:08
membersilent200713-Jul-08 22:08 
GeneralThanks Pin
PALANI KUMAR12-Mar-08 23:45
memberPALANI KUMAR12-Mar-08 23:45 
GeneralCotrolStyle.DoubleBuffer Pin
vadivelkumar5-Jul-07 21:26
membervadivelkumar5-Jul-07 21:26 
QuestionBackgound Objects? Pin
Doncp7-Dec-06 6:43
memberDoncp7-Dec-06 6:43 
AnswerRe: Backgound Objects? Pin
zhaoxiong198629-Aug-10 17:32
memberzhaoxiong198629-Aug-10 17:32 
GeneralThanks! Pin
minity30-Jul-06 2:51
memberminity30-Jul-06 2:51 
GeneralDrawing.... Pin
zmrcic13-Mar-06 3:06
memberzmrcic13-Mar-06 3:06 
Generalif ((width != this.width) || (height != this.height)) Pin
Marijn Stevens12-Mar-06 12:06
memberMarijn Stevens12-Mar-06 12:06 
GeneralRe: if ((width != this.width) || (height != this.height)) Pin
supercali4-Apr-06 14:51
membersupercali4-Apr-06 14:51 
GeneralStill having issues Pin
Ponsonby23-Jan-06 23:15
memberPonsonby23-Jan-06 23:15 
GeneralNice Job! Pin
jinzhecheng30-Nov-05 4:31
memberjinzhecheng30-Nov-05 4:31 
GeneralFlickers worse than before, I must be missing something Pin
sklett2-Oct-05 7:23
membersklett2-Oct-05 7:23 
GeneralRe: Flickers worse than before, I must be missing something Pin
jaypea23-Nov-05 3:48
memberjaypea23-Nov-05 3:48 
GeneralRe: Flickers worse than before, I must be missing something Pin
jaypea28-Nov-05 5:23
memberjaypea28-Nov-05 5:23 
GeneralRe: Flickers worse than before, I must be missing something Pin
jaypea28-Nov-05 5:24
memberjaypea28-Nov-05 5:24 
GeneralRe: Flickers worse than before, I must be missing something Pin
LPlateDeveloper7-Oct-08 23:15
memberLPlateDeveloper7-Oct-08 23:15 
GeneralRe: Flickers worse than before, I must be missing something Pin
dxwnd6-Jan-10 18:42
memberdxwnd6-Jan-10 18:42 
GeneralThanks Pin
idda19-Sep-05 22:19
memberidda19-Sep-05 22:19 
Generaladded reset method Pin
togil28-Jul-05 1:14
membertogil28-Jul-05 1:14 
GeneralRe: added reset method Pin
norm.net28-Jul-05 1:24
membernorm.net28-Jul-05 1:24 
GeneralRe: added reset method Pin
jmanson19-Dec-05 17:03
memberjmanson19-Dec-05 17:03 
GeneralRe: added reset method Pin
norm.net19-Dec-05 20:36
membernorm.net19-Dec-05 20:36 
GeneralOn minimize event.. Pin
day13t1-Mar-05 22:44
memberday13t1-Mar-05 22:44 
GeneralRe: On minimize event.. Pin
pphc13-Jun-05 16:05
memberpphc13-Jun-05 16:05 
GeneralRe: On minimize event.. Pin
norm.net19-Sep-05 22:36
membernorm.net19-Sep-05 22:36 
GeneralRe: On minimize event.. Pin
ChrisWard31-May-07 1:19
memberChrisWard31-May-07 1:19 
GeneralRe: On minimize event.. Pin
norm .net31-May-07 1:24
membernorm .net31-May-07 1:24 
GeneralVery good piece of code much stronger then double buffering Pin
asherab2-Jan-05 21:40
memberasherab2-Jan-05 21:40 
GeneralA better method (?) Pin
TeisDraiby15-Oct-04 15:35
memberTeisDraiby15-Oct-04 15:35 
GeneralOops! Pin
TeisDraiby15-Oct-04 15:44
memberTeisDraiby15-Oct-04 15:44 
GeneralRe: A better method (!!!) Pin
Benoit Nesme27-Jan-06 0:48
memberBenoit Nesme27-Jan-06 0:48 
QuestionCan this be done in VB.NET?? Pin
RichiLloyd28-Sep-04 16:16
memberRichiLloyd28-Sep-04 16:16 
AnswerRe: Can this be done in VB.NET?? Pin
RichiLloyd28-Sep-04 16:18
memberRichiLloyd28-Sep-04 16:18 
Questionbuggy built in double buffering? Pin
sir_mad5-May-04 5:12
membersir_mad5-May-04 5:12 

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

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

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170924.2 | Last Updated 29 Jul 2003
Article Copyright 2003 by NormDroid
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid