Click here to Skip to main content
Click here to Skip to main content

Double Buffering With GDI+

By , 11 Feb 2002
 
<!-- Download Links --> <!-- Article image -->

Sample Image - GDIPlus.jpg

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

Introduction

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.top,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));
		graph->DrawLine(&pen,0,0,x,y);
        	}
	}
   // Draw the altered image.
   graphics.DrawImage(&bmp,rect.left,rect.top,rect.right,rect.bottom);

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.

License

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

Mazdak
Web Developer
Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5membermanoj kumar choubey16-Feb-12 23:53 
GeneralMemory leakmemberold6714-Feb-08 12:57 
Generaldouble bufferingmembermartin_man24-Aug-05 15:43 
GeneralsalammemberdSolariuM11-Dec-04 19:26 
GeneralIt seem's thatmemberdSolariuM11-Dec-04 19:22 
GeneralMy Image Still Flickers!!!memberRassul Yunussov5-Sep-04 19:39 
I saw this message before, now i have to post my own. I tried All the ways of drawing with double buffer, and without success - my image still flickers!!!. I don't know what's wrong, and drawing takes all the performance of my processor. My friend uses the GDI+ in C# and he said that never had this problem. Am i doing something wrong??? For the first i tried to use Bitmap of GDI+, and it flicket, then i tried another way - to draw in one buffer and the with GDI function BitBlt draw this buffer on the screen, and it flicked again!!! Or i missunderstand something??? I want to make multiplication,i've made it in GDI, and it was good, but now i want to implement the antialiassing, that's why i decided to use GDI+. But i don't have a good animation. I hope you understand me.
Please HELP
 
I Found the PROBLEM!!!!!!!!!!!!!!!Smile | :)
It was in INVALIDATE()!!!!!!!
Now i avoid this function, and everything is allright now!!!!!!!!!!!
 

Generalit's not interesting to wait 36 seconds...membervisionz10-Dec-03 8:14 
GeneralRe: it's not interesting to wait 36 seconds...memberMazdak13-Dec-03 10:06 
GeneralGDI+ can't create a bitmap in memorymemberSam Klaneski4-Oct-03 5:15 
GeneralRe: GDI+ can't create a bitmap in memorymemberYixin Liu31-Dec-03 8:15 
GeneralHaving trouble replicating your ideamemberMHK14-May-03 13:40 
GeneralMemory Leakmembermarc_cohn23-Feb-03 11:49 
GeneralMy image still flickers!memberHidde Wallaart16-Aug-02 5:35 
GeneralRe: not any more!memberHidde Wallaart19-Aug-02 23:38 
GeneralHowever, this method will not work for a scroll view that is using GDI+.memberxxxyyyzzz6-Feb-03 17:43 
GeneralRe: not any more! > can't get it to autorefreshsussAnonymous24-Feb-03 9:22 
GeneralRe: not any more! &gt; can't get it to autorefreshmembermartybeavis5-Nov-03 12:14 
GeneralWS_EX_COMPOSITEDmemberswinefeaster11-Jun-02 20:15 
GeneralRe: WS_EX_COMPOSITEDmemberkowit_x26-Nov-02 10:33 
GeneralRe: WS_EX_COMPOSITEDmemberSwinefeaster27-Nov-02 21:43 
General.net gives you doublebuffering automaticallymemberAnonymous13-Feb-02 3:16 
GeneralRe: .net gives you doublebuffering automaticallymemberMazdak13-Feb-02 5:05 
GeneralRe: .net gives you doublebuffering automaticallysussAnonymous31-Jan-04 8:18 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130619.1 | Last Updated 12 Feb 2002
Article Copyright 2002 by Mazdak
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid