The most common image window flicker problem is encountered in recent WinForm projects involving the use of GDI to draw waveform graphs. Before going to the interview process was also asked how to solve the problem of image flicker during the drawing process.
Today on the actual project and the reading of some of the double-buffering technology of the article about their understanding of double buffering technology. The main reference article is from 7849880.
First, the cause of flicker
The form needs to be redrawn when the WM_PAINT message is started during the drawing process, the process first erases the background color of the form, and then draws the image to the form. Once the two actions do not occur at the same time, the first erase background color, the phenomenon of drawing images, it looks like the image has been flashing.
Second, the solution:
Use double buffering techniques to draw.
Three, the principle of double buffering technology:
The image is drawn in a buffer before the result of the buffer is output to the form. That is, when a window is refreshed, all elements are displayed to the window at the same time.
Four, the method of operating double buffering in C #.
1, using the default double buffering
(1) The easiest way to do this is through the default double buffering of forms and controls provided by the. Net Framework: Set form and control properties doublebuffered = true;
(2) Use the SetStyle method does not enable default double buffering for forms and controls. Add the settings related code to the constructor.
This method is also applied in the project.
2. Manually manage double buffering
Manually managing double buffering is a separate memory for double buffering, in which the drawing is completed and then displayed to the form.
Double buffering technology in C # GDI drawing