Click here to Skip to main content
6,635,160 members and growing! (17,542 online)
Email Password   helpLost your password?
Multimedia » GDI+ » General     Intermediate License: A Public Domain dedication

Flicker free drawing using GDI+ and C#

By Norm .net

Describes how to implement flicker free drawing using C# and GDI+.
C#, VC6, VC7, VC7.1.NET 1.1, Win2K, WinXP, Win2003, MFC, VS.NET2003, Dev
Posted:28 Jul 2003
Views:224,816
Bookmarked:120 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
36 votes for this article.
Popularity: 6.12 Rating: 3.93 out of 5
5 votes, 13.9%
1
3 votes, 8.3%
2
1 vote, 2.8%
3
5 votes, 13.9%
4
22 votes, 61.1%
5

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

About the Author

Norm .net


Member
Senior Software Engineer working on WPF/Winform projects.
Occupation: Software Developer (Senior)
Company: Software Kinetics
Location: United Kingdom United Kingdom

Other popular GDI+ articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 71 (Total in Forum: 71) (Refresh)FirstPrevNext
GeneralGood not perfect but good PinmemberCAD Zombie6:09 20 Oct '09  
GeneralDrawing an image without flicker Pinmemberpcbaby1516:41 16 Jul '09  
Generalthis.DoubleBuffered Pinmemberdfsdfsdfoyuti5:37 28 Feb '09  
GeneralRe: this.DoubleBuffered PinmemberNorm .net21:45 1 Mar '09  
GeneralCan i use this DBGraphics class for drwaing the controls on my form Pinmemberbhaskar soni2:27 5 Jan '09  
GeneralWhat license is the code under? Pinmemberbinary123022:28 1 Dec '08  
GeneralRe: What license is the code under? PinmemberNorm .net5:48 2 Dec '08  
GeneralRe: What license is the code under? Pinmemberbinary123019:30 2 Dec '08  
GeneralThis is a godsend Pinmemberwalentys23:45 2 Aug '08  
GeneralNice job! Pinmembersandeepstudd2:06 15 Jul '08  
Jokethank u very much Pinmembersilent200723:08 13 Jul '08  
GeneralThanks PinmemberPALANI KUMAR0:45 13 Mar '08  
GeneralCotrolStyle.DoubleBuffer Pinmembervadivelkumar22:26 5 Jul '07  
GeneralBackgound Objects? PinmemberDoncp7:43 7 Dec '06  
GeneralThanks! Pinmemberminity3:51 30 Jul '06  
GeneralDrawing.... Pinmemberzmrcic4:06 13 Mar '06  
Generalif ((width != this.width) || (height != this.height)) PinmemberMarijn Stevens13:06 12 Mar '06  
GeneralRe: if ((width != this.width) || (height != this.height)) Pinmembersupercali15:51 4 Apr '06  
GeneralStill having issues PinmemberPonsonby0:15 24 Jan '06  
GeneralNice Job! Pinmemberjinzhecheng5:31 30 Nov '05  
GeneralFlickers worse than before, I must be missing something Pinmembersklett8:23 2 Oct '05  
GeneralRe: Flickers worse than before, I must be missing something Pinmemberjaypea4:48 23 Nov '05  
GeneralRe: Flickers worse than before, I must be missing something Pinmemberjaypea6:23 28 Nov '05  
GeneralRe: Flickers worse than before, I must be missing something Pinmemberjaypea6:24 28 Nov '05  
GeneralRe: Flickers worse than before, I must be missing something PinmemberLPlateDeveloper0:15 8 Oct '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 28 Jul 2003
Editor: Marc Clifton
Copyright 2003 by Norm .net
Everything else Copyright © CodeProject, 1999-2009
Web18 | Advertise on the Code Project