Click here to Skip to main content
12,550,449 members (45,318 online)
Click here to Skip to main content
Add your own
alternative version


85 bookmarked

Double Buffering With GDI+

, 11 Feb 2002
Rate this:
Please Sign up or sign in to vote.
Simple example about Double Buffering with GDI+
<!-- Download Links --> <!-- Article image -->

Sample Image - GDIPlus.jpg

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


This is a simple example about GDI+ which I used in my first GDI+ project.This article is focused on Double Buffering. First if you want to add GDI+ supports to your project,you can refer to Starting with GDI+ By Christian Graus.
The main part of my example is in OnPaint function that you can see everything there,Image,Font,Brush and etc.They are very clear so I don't discuss about them.I only explain "Double Buffering" here.

As you know you can draw images into your windows and create a brush or pen from TextureBrush and draw shapes or line, using the image.Even you can draw a text using the images by supplying TextureBrush.(you can see the examples in this article or Christian Grause articles)

I think one of the most important use of images is Double Buffernig technique.This technique is used when the drawing we wish to create has many details,and it is time consuming to draw even with a fast computer.In this situation it seems the image creeps on to the screen while being drawn.For example in mapping applications or CAD/CAM applications we would encounter this problem. In this technique instead of drawing in the screen,first we draw into an image and then we draw the iamge into the window. Here is my example in double buffering:

srand( (unsigned)time( NULL ) );
int number = rand();
number /= RAND_MAX + 1;
number *= 254;

Rect rc(rect.left,,rect.right,rect.bottom);
   Bitmap bmp(rect.right,rect.bottom);

// Create a Graphics object that is associated with the image.
Graphics* graph = Graphics::FromImage(&bmp);

for(int x=0;x<rect.right;x++)
 for(int y=0; y<rect.bottom; y++)
     double number = rand();
     number /= RAND_MAX + 1;
     number *= 254;
     Pen pen(Color(number,number,number,number));
// Draw the altered image.

It takes 36 seconds to paint the screen on my computer(AMD 1.33GHtz and 256Mb RAM) without double buffering but only 5 seconds with this technique.You know,it is not interesting to wait 36 seconds each time that your window need to repaint!

Finally I want to thank Christian Graus for his supports and good articles about GDI+ which I used some of his codes and ideas.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Web Developer
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
manoj kumar choubey16-Feb-12 23:53
membermanoj kumar choubey16-Feb-12 23:53 
GeneralMemory leak Pin
old6714-Feb-08 12:57
memberold6714-Feb-08 12:57 
Generaldouble buffering Pin
martin_man24-Aug-05 15:43
membermartin_man24-Aug-05 15:43 
Generalsalam Pin
dSolariuM11-Dec-04 19:26
memberdSolariuM11-Dec-04 19:26 
GeneralIt seem's that Pin
dSolariuM11-Dec-04 19:22
memberdSolariuM11-Dec-04 19:22 
GeneralMy Image Still Flickers!!! Pin
Rassul Yunussov5-Sep-04 19:39
memberRassul Yunussov5-Sep-04 19:39 
Generalit's not interesting to wait 36 seconds... Pin
visionz10-Dec-03 8:14
membervisionz10-Dec-03 8:14 
GeneralRe: it's not interesting to wait 36 seconds... Pin
Mazdak13-Dec-03 10:06
memberMazdak13-Dec-03 10:06 
GeneralGDI+ can't create a bitmap in memory Pin
Sam Klaneski4-Oct-03 5:15
memberSam Klaneski4-Oct-03 5:15 
GeneralRe: GDI+ can't create a bitmap in memory Pin
Yixin Liu31-Dec-03 8:15
memberYixin Liu31-Dec-03 8:15 
This Article may be helpful!

HOWTO: Draw on a Memory Bitmap in GDI+PSS ID Number: Q299583

Article Last Modified on 09-18-2001

The information in this article applies to:

Microsoft Win32 Application Programming Interface (API)
the operating system: Microsoft Windows XP
the operating system: Microsoft Windows NT 4.0
the operating system: Microsoft Windows 2000
the operating system: Microsoft Windows 98
the operating system: Microsoft Windows Millennium Edition


You may sometimes want to obtain a bitmap or image that contains the image that was drawn by a graphics object.

One of the overloaded constructors for the Bitmap class takes a graphics object as a parameter. However, this constructor does not use the image drawn by the graphics object to initialize the bitmap bits. It simply creates a bitmap with properties similar to the graphics object, such as dots per inch.

More Information
Because the Bitmap constructor does not initialize the image bits by using the image from the graphics object, code like the following will NOT result in a bitmap that contains the image that was drawn by the Graphics object:

Graphics g( hWnd );
// Draw on g
Bitmap b( 100, 100, &g ); // Will not get image from g

To use a graphics object to draw on a bitmap, code like the following can be used instead:
Bitmap b(100,100);
Graphics *g = Graphics::FromImage(&b);
// Draw on g

To capture the preexisting image from a window, a Windows Graphics Device Interface (GDI) function such as BitBlt() or StretchBlt() would have to be used to copy the image from the screen to a memory bitmap. This memory bitmap could then be used in the overloaded Bitmap constructor, which takes an HBITMAP as a parameter.

For additional information on capturing the screen using GDI, click the article number below to view the article in the Microsoft Knowledge Base:

Q186736 HOWTO: Capture and Print an Entire Window
"Capturing an Image" in the GDI section of the Platform SDK Documentation:

Additional query words:

Keywords: kbDSupport
Issue Type: kbhowto
Technology: kbAudDeveloper kbWin32sSearch kbWin32API

lyx in China
GeneralHaving trouble replicating your idea Pin
MHK14-May-03 13:40
memberMHK14-May-03 13:40 
GeneralMemory Leak Pin
marc_cohn23-Feb-03 11:49
membermarc_cohn23-Feb-03 11:49 
GeneralMy image still flickers! Pin
Hidde Wallaart16-Aug-02 5:35
memberHidde Wallaart16-Aug-02 5:35 
GeneralRe: not any more! Pin
Hidde Wallaart19-Aug-02 23:38
memberHidde Wallaart19-Aug-02 23:38 
GeneralHowever, this method will not work for a scroll view that is using GDI+. Pin
xxxyyyzzz6-Feb-03 17:43
memberxxxyyyzzz6-Feb-03 17:43 
GeneralRe: not any more! > can't get it to autorefresh Pin
Anonymous24-Feb-03 9:22
sussAnonymous24-Feb-03 9:22 
GeneralRe: not any more! &gt; can't get it to autorefresh Pin
martybeavis5-Nov-03 12:14
membermartybeavis5-Nov-03 12:14 
swinefeaster11-Jun-02 20:15
memberswinefeaster11-Jun-02 20:15 
kowit_x26-Nov-02 10:33
memberkowit_x26-Nov-02 10:33 
Swinefeaster27-Nov-02 21:43
memberSwinefeaster27-Nov-02 21:43 gives you doublebuffering automatically Pin
Anonymous13-Feb-02 3:16
memberAnonymous13-Feb-02 3:16 
GeneralRe: .net gives you doublebuffering automatically Pin
Mazdak13-Feb-02 5:05
memberMazdak13-Feb-02 5:05 
GeneralRe: .net gives you doublebuffering automatically Pin
Anonymous31-Jan-04 8:18
sussAnonymous31-Jan-04 8:18 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161021.1 | Last Updated 12 Feb 2002
Article Copyright 2002 by Mazdak
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid