Click here to Skip to main content
15,887,336 members
Articles / Desktop Programming / MFC
Article

Flicker free drawing using GDI+ and C#

Rate me:
Please Sign up or sign in to vote.
4.31/5 (43 votes)
28 Jul 2003Public Domain2 min read 449.1K   8K   154   79
Describes how to implement flicker free drawing using C# and GDI+.

Sample Image - flickerFreeDrawing.jpg

Introduction

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.

Background

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
        memGraphics.Render(e.Graphics);
    }
}

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.

History

V1.0 Article creation.

License

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


Written By
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
  • ASP.net


Visit Software Kinetics

Comments and Discussions

 
QuestionHow to avoid flickering on textbox? or treeview control? Pin
franklinzabala15-Oct-03 4:52
franklinzabala15-Oct-03 4:52 
AnswerRe: How to avoid flickering on textbox? or treeview control? Pin
f1bp31-Jan-07 9:29
f1bp31-Jan-07 9:29 
Questionbut, why it's necessary? Pin
17-Sep-03 4:26
suss17-Sep-03 4:26 
AnswerRe: but, why it's necessary? Pin
Anthony_Yio3-Mar-04 14:34
Anthony_Yio3-Mar-04 14:34 
GeneralGreat work !!! Pin
FredyAlfredo18-Aug-03 3:00
FredyAlfredo18-Aug-03 3:00 
GeneralRe: Great work !!! Pin
Anonymous19-Aug-03 5:08
Anonymous19-Aug-03 5:08 
GeneralRe: Great work !!! Pin
FredyAlfredo20-Aug-03 2:39
FredyAlfredo20-Aug-03 2:39 
GeneralIsn't this build in Pin
JacksonBrown196030-Jul-03 4:22
JacksonBrown196030-Jul-03 4:22 
I am new to C# and .NET but when I want my controls to paint with double buffering I just use the following settings

SetStyle(ControlStyles.UserPaint, true);
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
GeneralRe: Isn't this build in Pin
NormDroid30-Jul-03 9:32
professionalNormDroid30-Jul-03 9:32 
GeneralRe: Isn't this build in Pin
leppie30-Jul-03 10:55
leppie30-Jul-03 10:55 
GeneralRe: Isn't this build in Pin
NormDroid30-Jul-03 20:50
professionalNormDroid30-Jul-03 20:50 
GeneralRe: Isn't this build in Pin
Arun Bhalla5-Aug-03 9:47
Arun Bhalla5-Aug-03 9:47 
GeneralRe: Isn't this build in Pin
NormDroid30-Jul-03 20:53
professionalNormDroid30-Jul-03 20:53 
GeneralRe: Isn't this build in Pin
swythan30-Jul-03 23:28
swythan30-Jul-03 23:28 
GeneralRe: Isn't this build in - Well, sort of... Pin
J. Dunlap5-Aug-03 10:17
J. Dunlap5-Aug-03 10:17 
QuestionAm I missing something? Pin
garythom_work30-Jul-03 3:29
garythom_work30-Jul-03 3:29 
AnswerRe: Am I missing something? Pin
NormDroid30-Jul-03 3:50
professionalNormDroid30-Jul-03 3:50 
GeneralMissing file in the project Pin
PatrickLambert29-Jul-03 21:00
PatrickLambert29-Jul-03 21:00 
GeneralRe: Missing file in the project Pin
NormDroid29-Jul-03 22:02
professionalNormDroid29-Jul-03 22:02 
Generalproperty Pin
Zibar29-Jul-03 20:59
sussZibar29-Jul-03 20:59 
GeneralRe: property Pin
NormDroid29-Jul-03 22:05
professionalNormDroid29-Jul-03 22:05 

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.