A summary of C # drawing double buffering Technology

Source: Internet
Author: User
Tags foreach config
GDI + double buffering problem finally finished, really relieved! Misunderstanding:. net1.1 and. NET 2.0 differ in processing control double buffering.. NET 1.1, using: this. SetStyle (Controlstyles.doublebuffer, true);. NET 2.0, using: this. SetStyle (Controlstyles.optimizeddoublebuffer, true); no, not always. Prompt parameter is invalid, has not known is this question, hehe must know, the entity does not flicker the realization and the entity drawing method does not have how many relations, Only the drawing method can control the refresh area of the entity and make the double buffering performance more excellent! analysis of key causes of screen flicker: One, draw the window because the size position state changes to redraw the operationdrawing window content or size every time you change, you invoke the Paint event to redraw the action, which refreshes the screen once to maintain the normal display of the window. The refresh process causes all entities to be redrawn, and the redrawing of individual entities does not cause the paint event to occur, so each refresh of the window invokes only the Paint event once.      When the window is refreshed once, the redraw of each entity is immediately displayed to the window, so the entire window, as long as the entity is in the position, are refreshed, and the refresh time is different, flashing phenomenon will naturally occur.     Therefore, the key factor that causes window flicker at this time is not the number of paint event calls, but the redrawing of individual entities. According to the above analysis, when the number of entities is not long, window refresh position is not much, the window flicker effect is not serious; When the number of entities is large, the drawing window redraw the number of entities, the drawing window every time the refresh will cause more entities to redraw, the window more positions are refreshed, The flickering phenomenon will naturally become more and more serious.     In particular, when the size of the larger drawing time is longer, the flicker problem will be more serious, because the time delay will be longer. The key to solving the above problems lies in: when the window is refreshed once, all entities are displayed to the window at the same time. second, the mouse tracking drawing operations or to the entity deformation operationThe Paint event occurs frequently when a mouse-tracking drawing operation or an entity is distorted, which increases the number of times the window refreshes. Although all entities are displayed to the window at the same time as the window refreshes, there is also a time delay because the window refreshes at this time much less than the time it takes for the entity to display to the window.     Therefore the flicker phenomenon cannot eliminate completely!     So, the key factor that causes window flicker at this time is how many times the paint event occurs. The key to solving this problem is: set several key properties for a form or control. Here are the specifics of the solution: Key technologies to address double buffering:1, set the Display entity control several properties: must be set, otherwise the effect is not very obvious! This.                       SetStyle (Controlstyles.optimizeddoublebuffer |                     Controlstyles.resizeredraw |    Controlstyles.allpaintinginwmpaint, True); 2, so that all entities are displayed to the window at the same time as the window refreshes. This can be done in several ways, both of which involve the way graphics objects are created. graphics How objects are created:A, create a graphics object on this canvas by creating a canvas with the same size as the display control on the memory.  Then all the entities are drawn on this canvas, and then use the canvas to overwrite the background of the display control to achieve the "show once only refresh" effect!  Implementation code (in the OnPaint method): Rectangle rect = e.cliprectangle; Bitmap bufferimage = new Bitmap (this. Width, this. Height);
Graphics g = graphics.fromimage (bufferimage); G.clear (this. BackColor);
G.smoothingmode = smoothingmode.highquality; Quality
G.pixeloffsetmode = pixeloffsetmode.highquality; High pixel offset mass foreach (IShape drawobject in doc.drawobjectlist)
{if (rect). Intersectswith (drawobject. Rect))
{
drawobject. Draw (g);
if (drawobject. Trackerstate = = Config. Module.Core.TrackerState.Selected
&& this. Currentoperator = = Enum.Operator.Transfrom)//Only display entity hotspots when editing node operations
{
drawobject. Drawtracker (g);
}
}

using (Graphics TG = e.graphics)
{
Tg.  DrawImage (bufferimage, 0, 0); Put the canvas on the screen
b, create the Graphics object directly on memory: Rectangle rect = e.cliprectangle; BufferedGraphicsContext currentcontext = bufferedgraphicsmanager.current;
BufferedGraphics Mybuffer = currentcontext.allocate (E.graphics, E.cliprectangle);
Graphics g = mybuffer.graphics;
G.smoothingmode = smoothingmode.highquality;
G.pixeloffsetmode = Pixeloffsetmode.highspeed;
G.clear (this. BackColor);
foreach (IShape drawobject in doc.drawobjectlist)
{
if (rect. Intersectswith (drawobject. Rect))
{
drawobject. Draw (g);
if (drawobject. Trackerstate = = Config. Module.Core.TrackerState.Selected
&& this. Currentoperator = = Enum.Operator.Transfrom)//Only display entity hotspots when editing node operations
{
drawobject. Drawtracker (g);
}
}
} mybuffer.render (E.graphics);
G.dispose ();
Mybuffer.dispose ()//releasing resources to this point, double buffer problem resolution, the two ways to achieve the same effect, but the last way to occupy a small amount of memory, will not appear memory leaks! References: 1, http://dev.yesky.com/msdn/148/2596148.shtml2. GDI + graphics program design []mahesh Chand Electronics Publishing House 3, C # Advanced Programming (Third edition)

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.