13,085,123 members (55,412 online)
alternative version

#### Stats

203.5K views
88 bookmarked
Posted 11 Feb 2002

# Double Buffering With GDI+

, 11 Feb 2002
 Rate this:
Simple example about Double Buffering with GDI+

## Introduction

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.

A list of licenses authors might use can be found here

## Share

 Web Developer Iran (Islamic Republic of)
No Biography provided

## You may also be interested in...

 First Prev Next
 My vote of 5 manoj kumar choubey16-Feb-12 23:53 manoj kumar choubey 16-Feb-12 23:53
 Memory leak old6714-Feb-08 12:57 old67 14-Feb-08 12:57
 double buffering martin_man24-Aug-05 15:43 martin_man 24-Aug-05 15:43
 salam dSolariuM11-Dec-04 19:26 dSolariuM 11-Dec-04 19:26
 It seem's that dSolariuM11-Dec-04 19:22 dSolariuM 11-Dec-04 19:22
 My Image Still Flickers!!! Rassul Yunussov5-Sep-04 19:39 Rassul Yunussov 5-Sep-04 19:39
 it's not interesting to wait 36 seconds... visionz10-Dec-03 8:14 visionz 10-Dec-03 8:14
 Re: it's not interesting to wait 36 seconds... Mazdak13-Dec-03 10:06 Mazdak 13-Dec-03 10:06
 GDI+ can't create a bitmap in memory Sam Klaneski4-Oct-03 5:15 Sam Klaneski 4-Oct-03 5:15
 Re: GDI+ can't create a bitmap in memory Yixin Liu31-Dec-03 8:15 Yixin Liu 31-Dec-03 8:15
 Having trouble replicating your idea MHK14-May-03 13:40 MHK 14-May-03 13:40
 Memory Leak marc_cohn23-Feb-03 11:49 marc_cohn 23-Feb-03 11:49
 My image still flickers! Hidde Wallaart16-Aug-02 5:35 Hidde Wallaart 16-Aug-02 5:35
 Re: not any more! Hidde Wallaart19-Aug-02 23:38 Hidde Wallaart 19-Aug-02 23:38
 However, this method will not work for a scroll view that is using GDI+. xxxyyyzzz6-Feb-03 17:43 xxxyyyzzz 6-Feb-03 17:43
 Re: not any more! > can't get it to autorefresh Anonymous24-Feb-03 9:22 Anonymous 24-Feb-03 9:22
 Re: not any more! > can't get it to autorefresh martybeavis5-Nov-03 12:14 martybeavis 5-Nov-03 12:14
 WS_EX_COMPOSITED swinefeaster11-Jun-02 20:15 swinefeaster 11-Jun-02 20:15
 Re: WS_EX_COMPOSITED kowit_x26-Nov-02 10:33 kowit_x 26-Nov-02 10:33
 Re: WS_EX_COMPOSITED Swinefeaster27-Nov-02 21:43 Swinefeaster 27-Nov-02 21:43
 .net gives you doublebuffering automatically Anonymous13-Feb-02 3:16 Anonymous 13-Feb-02 3:16
 Re: .net gives you doublebuffering automatically Mazdak13-Feb-02 5:05 Mazdak 13-Feb-02 5:05
 Re: .net gives you doublebuffering automatically Anonymous31-Jan-04 8:18 Anonymous 31-Jan-04 8:18
 Last Visit: 31-Dec-99 18:00     Last Update: 16-Aug-17 4:12 Refresh 1