Dual buffering solves the problem of Drawing Image flickering

Source: Internet
Author: User

Implementation Methods in C ++ builder:

Principle: Double Buffering creates an object in the memory that is consistent with the drawing area on the screen. First, the image is drawn to the object in the memory,
Copy the image on this object to the screen at one time, which can greatly speed up the drawing.

The dual-buffer implementation process is as follows:

1. Create a buffer consistent with the canvas in the memory

Bufferbmp = new graphics: tbitmap (); // creates a bitmap.
Bufferbmp-> canvas-> handle = createcompatibledc (canvas-> handle); // create a compatible DC
Bufferbmp-> width = width; // you can specify the bitmap width.
Bufferbmp-> Height = height; // sets the bitmap height.

2. drawing in the buffer zone

Bufferbmp-> canvas-> brush-> color = clbtnface; // you can specify the paint brush color.
Bufferbmp-> canvas-> fillrect (rect (, width, height); // clear the background
Bufferbmp-> canvas-> moveTo (...,....);
// Add your image below

3. Copy the buffer bitmap to the current canvas.
Bitblt (canvas-> handle, 0, 0, width, height, bufferbmp-> canvas-> handle, 0, 0, srccopy );

4. Release the memory buffer
Delete bufferbmp;

The dual-buffering method in VC:

In addition to displaying images on the screen, the dual-buffer mode also displays images in the memory. We can plot the image to be displayed in the memory first, and then overwrite the image in the memory to the screen one by one point at a time (this process is very fast, because it is a very regular copy of memory ). In this way, when drawing in the memory, any background color with a large contrast will not flash, because it cannot be seen. When it is attached to the screen, the final image in the memory is slightly different from the screen display image (if there is no motion, of course there is no difference), so that it will not flash.
In addition to displaying images on the screen, the dual-buffer mode also displays images in the memory. We can plot the image to be displayed in the memory first, and then overwrite the image in the memory to the screen one by one point at a time (this process is very fast, because it is a very regular copy of memory ). In this way, when drawing in the memory, any background color with a large contrast will not flash, because it cannot be seen. When it is attached to the screen, the final image in the memory is slightly different from the screen display image (if there is no motion, of course there is no difference), so that it will not flash.

// In ondraw (CDC * PDC:

CDC memdc; // define a display device object
Cbitmap membitmap; // defines a bitmap object.

// Create a memory display device compatible with Screen Display
Memdc. createcompatibledc (null );
// No drawing yet, because there is no place to draw ^_^
// A bitmap compatible with screen display is created below. The size of the bitmap can be set to the size of the window.
Membitmap. createcompatiblebitmap (PDC, nwidth, nheight );

// Select the bitmap to the memory display device.
// Only the memory display device with the bitmap selected can draw a local image and draw it to the specified bitmap.
Cbitmap * poldbit = memdc. SelectObject (& membitmap );

// Use the background color to clear the bitmap. Here, I use white as the background color.
// You can also use your own color
Memdc. fillsolidrect (255,255,255, nwidth, nheight, RGB ));

// Drawing
Memdc. moveTo (......);
Memdc. lineto (......);

// Copy the image in the memory to the screen for display
PDC-> bitblt (0, 0, nwidth, nheight, & memdc, 0, srccopy );

// Cleanup after drawing is complete
Membitmap. deleteobject ();
Memdc. deletedc ();

Supplement: If you want to set the doublebuffer attribute of the canvas to true in Delphi, enable double buffering;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.