5,692,513 members and growing! (16,403 online)
Email Password   helpLost your password?
Multimedia » GDI+ » General     Beginner

Double Buffering With GDI+

By Mazdak

Simple example about Double Buffering with GDI+
VC6, VC7, C++Windows, Win2K, WinXP, MFC, GDI+, VS.NET2002, Visual Studio, Dev

Posted: 11 Feb 2002
Updated: 11 Feb 2002
Views: 116,084
Bookmarked: 30 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
19 votes for this Article.
Popularity: 5.31 Rating: 4.15 out of 5
2 votes, 18.2%
1
0 votes, 0.0%
2
1 vote, 9.1%
3
2 votes, 18.2%
4
6 votes, 54.5%
5

Sample Image - GDIPlus.jpg

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



Occupation: Web Developer
Location: Iran, Islamic Republic Of Iran, Islamic Republic Of

Other popular GDI+ articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
GeneralMemory leakmemberold6713:57 14 Feb '08  
Generaldouble bufferingmembermartin_man16:43 24 Aug '05  
GeneralsalammemberdSolariuM20:26 11 Dec '04  
GeneralIt seem's thatmemberdSolariuM20:22 11 Dec '04  
GeneralMy Image Still Flickers!!!memberRassul Yunussov20:39 5 Sep '04  
Generalit's not interesting to wait 36 seconds...membervisionz9:14 10 Dec '03  
GeneralRe: it's not interesting to wait 36 seconds...memberMazdak11:06 13 Dec '03  
GeneralGDI+ can't create a bitmap in memorymemberSam Klaneski6:15 4 Oct '03  
GeneralRe: GDI+ can't create a bitmap in memorymemberYixin Liu9:15 31 Dec '03  
GeneralHaving trouble replicating your ideamemberMHK14:40 14 May '03  
GeneralMemory Leakmembermarc_cohn12:49 23 Feb '03  
GeneralMy image still flickers!memberHidde Wallaart6:35 16 Aug '02  
GeneralRe: not any more!memberHidde Wallaart0:38 20 Aug '02  
GeneralHowever, this method will not work for a scroll view that is using GDI+.memberxxxyyyzzz18:43 6 Feb '03  
GeneralRe: not any more! > can't get it to autorefreshsussAnonymous10:22 24 Feb '03  
GeneralRe: not any more! > can't get it to autorefreshmembermartybeavis13:14 5 Nov '03  
GeneralWS_EX_COMPOSITEDmemberswinefeaster21:15 11 Jun '02  
GeneralRe: WS_EX_COMPOSITEDmemberkowit_x11:33 26 Nov '02  
GeneralRe: WS_EX_COMPOSITEDmemberSwinefeaster22:43 27 Nov '02  
General.net gives you doublebuffering automaticallymemberAnonymous4:16 13 Feb '02  
GeneralRe: .net gives you doublebuffering automaticallymemberMazdak6:05 13 Feb '02  
GeneralRe: .net gives you doublebuffering automaticallysussAnonymous9:18 31 Jan '04  

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

PermaLink | Privacy | Terms of Use
Last Updated: 11 Feb 2002
Editor: Chris Maunder
Copyright 2002 by Mazdak
Everything else Copyright © CodeProject, 1999-2008
Web15 | Advertise on the Code Project