The double buffering technology drawing based on C #

Source: Internet
Author: User

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 #

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.