Click here to Skip to main content
15,886,724 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 449K   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

 
Generaladded reset method Pin
togil28-Jul-05 1:14
togil28-Jul-05 1:14 
GeneralRe: added reset method Pin
NormDroid28-Jul-05 1:24
professionalNormDroid28-Jul-05 1:24 
GeneralRe: added reset method Pin
jmanson19-Dec-05 17:03
jmanson19-Dec-05 17:03 
GeneralRe: added reset method Pin
NormDroid19-Dec-05 20:36
professionalNormDroid19-Dec-05 20:36 
GeneralOn minimize event.. Pin
pacharakeng1-Mar-05 22:44
pacharakeng1-Mar-05 22:44 
GeneralRe: On minimize event.. Pin
pphc13-Jun-05 16:05
pphc13-Jun-05 16:05 
GeneralRe: On minimize event.. Pin
NormDroid19-Sep-05 22:36
professionalNormDroid19-Sep-05 22:36 
GeneralRe: On minimize event.. Pin
ChrisWard31-May-07 1:19
ChrisWard31-May-07 1:19 
I've been working on a double buffered control and encountered these problems when i put the control inside of a form. The best solution is change the CreateDoubleBuffer as below.

private bool CreateDoubleBuffer(Graphics g, int width, int height)<br />
		{<br />
			if (width == 0 || height == 0)<br />
				return false;<br />
			<br />
			if ((width != m_width) || (height != m_height))<br />
			{<br />
				if (m_memoryBitmap != null)<br />
				{<br />
					m_memoryBitmap.Dispose();<br />
					m_memoryBitmap = null;<br />
				}<br />
<br />
				if (m_graphics != null)<br />
				{<br />
					m_graphics.Dispose();<br />
					m_graphics = null;<br />
				}<br />
<br />
				m_width = width;<br />
				m_height = height;<br />
<br />
				m_memoryBitmap = new Bitmap(width, height);<br />
				m_graphics = Graphics.FromImage(m_memoryBitmap);<br />
			}<br />
<br />
			return true;<br />
		}


The final step is inside the resize event. Change it to be as follows.

private void DoubleBufferedControl_Resize(object sender, EventArgs e)<br />
		{<br />
			if (CreateDoubleBuffer( this.CreateGraphics(), this.ClientRectangle.Width, this.ClientRectangle.Height ) && this.Visible)<br />
			{<br />
				this.Invalidate();<br />
			}<br />
		}

GeneralRe: On minimize event.. Pin
NormDroid31-May-07 1:24
professionalNormDroid31-May-07 1:24 
GeneralVery good piece of code much stronger then double buffering Pin
mud772-Jan-05 21:40
mud772-Jan-05 21:40 
GeneralA better method (?) Pin
TeisDraiby15-Oct-04 15:35
TeisDraiby15-Oct-04 15:35 
GeneralOops! Pin
TeisDraiby15-Oct-04 15:44
TeisDraiby15-Oct-04 15:44 
GeneralRe: A better method (!!!) Pin
Benoit Nesme27-Jan-06 0:48
Benoit Nesme27-Jan-06 0:48 
QuestionCan this be done in VB.NET?? Pin
RichiLloyd28-Sep-04 16:16
RichiLloyd28-Sep-04 16:16 
AnswerRe: Can this be done in VB.NET?? Pin
RichiLloyd28-Sep-04 16:18
RichiLloyd28-Sep-04 16:18 
Questionbuggy built in double buffering? Pin
SiliconMind5-May-04 5:12
SiliconMind5-May-04 5:12 
Generalflicker free transparent window settings Pin
jwthomp16-Apr-04 9:34
jwthomp16-Apr-04 9:34 
GeneralRe: flicker free transparent window settings Pin
BradCalhoun18-Jul-04 9:09
BradCalhoun18-Jul-04 9:09 
GeneralRe: flicker free transparent window settings Pin
brucemo22-Sep-05 19:40
brucemo22-Sep-05 19:40 
GeneralRe: flicker free transparent window settings Pin
Graham Harrison4-Jan-06 11:06
Graham Harrison4-Jan-06 11:06 
GeneralRe: flicker free transparent window settings Pin
Mr Shelltoes23-Jan-06 23:42
Mr Shelltoes23-Jan-06 23:42 
Generalobject cleanup Pin
dzCepheus26-Dec-03 17:07
dzCepheus26-Dec-03 17:07 
GeneralRe: object cleanup Pin
NormDroid28-Jul-05 1:22
professionalNormDroid28-Jul-05 1:22 
GeneralFormBorderStyle=None Pin
Zebster21-Oct-03 22:42
Zebster21-Oct-03 22:42 
QuestionHow to avoid flickering on textbox? or treeview control? Pin
franklinzabala15-Oct-03 4:52
franklinzabala15-Oct-03 4:52 

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.