The key causes of flicker are:. When the window is refreshed, the redraw of each element is immediately displayed to the window, so the entire window, as long as it is in the location of the entity, is refreshed, and the time of the refresh is different, flashing phenomenon will naturally occur. therefore, the key factor that causes the window flicker is not the number of times the paint event is called, but the redrawing of the individual elements.
therefore , when the number of graphs is not long, the window refresh position is not much, the window flicker effect is not serious; When the number of elements is larger, the drawing window to redraw the number of elements increased, the drawing window each refresh will result in more entities redraw, the more locations of the window are refreshed, The phenomenon of flashing will naturally become more and more serious. In particular, when the large drawing time of the entity is longer, the flicker problem is more serious because the time delay is longer.
The key to solving the above problem is to have all the entities display to the window at the same time as the window is refreshed. So we need to use double buffering technology. The so-called double buffering technique is that the entities that are going to be drawn are now drawn in memory, and then the form that draws the good elements is drawn, realizing that all the elements are displayed to the window at the same time, eliminating flicker.
The double buffering Technique C # is implemented as follows:
1. Set the drawing style:
This. SetStyle (controlstyles.optimizeddoublebuffer| controlstyles.resizeredraw| Controlstyles.allpaintinginwmpaint,true);
2. Draw an entity in memory:
Bitmap Bmp=null;
Graphics G_bmp=null;
Bmp=new Bitmap (this. Width,this. Height);
G_bmp=graphics.fromimage (BMP);
G_bmp. Clear (this. BackColor);
G_bmp. DrawString ("Redraw", this.) Font,new SolidBrush (this. ForeColor), this. Location.x+1,this. LOCATION.Y+1);
This. Refresh ();
3. Implement the drawing on the form:
private void This_paint (Object Sender,painteventargs e)
{
Graphics G=e.graphics;
if (g==null) return;
if (g_bmp!=null)
{
G.drawimage (Image) bmp,0,0);
}
}
The double buffering technology drawing based on C #