C # GDI + dual-buffer technology

Source: Internet
Author: User
Dual-buffer problem of GDI +

I think many people who are engaged in graphics will use dual-buffer technology, and sometimes she is a headache. Recently, I also want to use dual-buffer technology,ProgramDebugging is not suitable. If you want to move the image, flickering jitter will always occur. I found some information on the Internet, but I was not clear about it. I didn't get it out after a night of hard work. I decided to study it myself the next day. Now I will share some of my ideas with you.

Basic Principles of Dual-buffering: (transfer)

A long-standing misunderstanding:. net1.1 and. NET 2.0 differ in processing controls in double buffering.
In. NET 1.1, use: This. setstyle (controlstyles. doublebuffer, true );
In. NET 2.0, use: This. setstyle (controlstyles. optimizeddoublebuffer, true );
I don't know if the parameter is always invalid.

You need to know that there is no relationship between the implementation of non-blinking elements and the method of drawing elements, but the method can control the refresh area of the elements, so that the double buffering performance is better!

Analysis of the key causes of flickering images:

1. During the repainting operation due to the size and position status changes in the drawing window

Each time the content or size of the drawing window changes, you must call the paint event to repaint the screen. This operation will refresh the screen once again to maintain normal display of the window. During the refresh process, all elements will be re-drawn, and the re-painting operation of each element will not cause the painting event. Therefore, each refresh of the window will only call the painting event once. During a Window refresh, the re-painting of each element is immediately displayed in the window. Therefore, as long as the position of the element in the window is refreshed, however, the refresh time is different, and flickering occurs naturally. Therefore, the key factor that causes the window to flash at this time is not the number of paint event calls, but the re-painting of each element.

According to the above analysis, when the number of elements is small, there are not many windows to refresh, and the flash effect is not serious. When the number of elements is large, the number of elements for repainting in the drawing window increases, every refresh in the drawing window will lead to a large number of elements to be re-drawn. The window is refreshed in many locations, and flickering will naturally become more and more serious. In particular, if the element is relatively large and the painting time is relatively long, the flash problem will be more serious, because the time delay will be longer.

The key to solving the preceding problem is that all elements are displayed in the window at the same time when the window is refreshed.
2. When you move the mouse to draw or perform deformation operations on the Elements

When you use the mouse to track and draw objects or perform deformation on elements, the paint event occurs frequently, which greatly increases the number of refresh windows. Although all elements are displayed in the window at the same time during a Window refresh, there is also a time delay, because the interval between window refreshes is much less than the time each time the elements are displayed in the window. Therefore, flashes cannot be completely eliminated! Therefore, the key factor that causes the window to flash at this time is the number of times the paint event occurs.
The key to solving this problem is:Set several key properties of a form or control.

The following describes the specific implementation methods: (transfer)

1. Create a "virtual canvas" in the memory: bitmap BMP = new Bitmap (600,600 );
2. Obtain graphics reference of the memory canvas: Graphics G = graphics. fromimage (BMP );

3. Drawing on the memory canvas: G. drawline (add parameters );

4. Draw out the memory in the window: This. creategraphics (). drawimage (BMP, 0, 0 );

Add the following to the constructor:Code

Code 1:

Setstyle (controlstyles. userpaint, true );
Setstyle (controlstyles. allpaintinginwmpaint, true ); // Do not erase background.
Setstyle (controlstyles. doublebuffer, true ); // Double Buffering

Or code 2:

This. setstyle (controlstyles. doublebuffer | Controlstyles. userpaint |
Controlstyles. allpaintinginwmpaint, true );
This. updatestyles ();

(Reprinted)

The above method is suitable for drawing images directly on the form, and it is easy to do. But sometimes we need to draw a chart on a certain control. What should we do? The principle is the same as drawing a graph directly on a form using double buffering. You also need to set the above Code 1 or code 2 in the control constructor. So how can we set it? By reading msdn, I found a custom control method and set it in the control constructor. In the appendix below, I will explain how to do it.

In Microsoft Visual Studio 2005, the C # language is used and the GDI + language is used. The goal is to achieve a simple draw line by dragging the mouse, and re-draw all the previously drawn lines.
The entire program uses three controls: A splitcontainer control, a custom panel control, and a panel control that comes with. The size of the splitcontainer control is set to form width, half form height, and positioned in the lower half of the form. Both the custom panel control and the panel control that comes with vs bind them to the Panel1 and panel2 of the splitcontainer control by setting their dock properties. The appendix describes how custom panel controls are defined.
The upper part of the form uses double buffering. The custom panel control uses double buffering, which is achieved by setting the style when you define the panel control (the setting method is the same as the double buffering setting method for the form, as shown in the following three statements), this cannot be set directly in the panel paint method, because setstyle () is not a public method in the panel class. The panel control provided by vs does not use double buffering.

Setstyle (controlstyles. allpaintinginwmpaint, true );
Setstyle (controlstyles. userpaint, true );
Setstyle (controlstyles. optimizeddoublebuffer, true );

I have integrated the three into one. I think you will be able to understand the principles, implementations, and effects of double buffering ,. If you are not very clear, you can leave a message for us to discuss.

there are two ways to create a graphics object: first, create a canvas of the same size as the display area or control on the memory, create a graphics object on the canvas. Then, all the elements are drawn on the canvas. After the painting is complete, use the canvas to overwrite the background of the display control, so that the display is refreshed only once! The second is to create a graphics object directly in the memory.

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.