Double Buffer technology (C # GDI)

Source: Internet
Author: User

C # How to Implement the form flash protection function. Everyone will think of using the dual-buffer technology, so how is it done in C?

1. Use the default Double Buffer

(1) the easiest way to use double buffering in applications is to use. NET Framework to provide default double buffering for forms and controls. Set the doublebuffered attribute to true.

This. doublebuffered = true;

(2) The setstyle method can be used to enable default double buffering for Windows Forms and the created Windows controls.

Setstyle (controlstyles. optimizeddoublebuffer, true );

2. manually set double buffering

. Netframework provides a class bufferedgraphicscontext to allocate and manage the graphic buffer separately. Each application domain has its own default bufferedgraphicscontext instance to manage all default dual-buffering for this application. In most cases, each application has only one application domain, so each application usually has only one default bufferedgraphicscontext. The bufferedgraphicscontext instance is managed by the bufferedgraphicsmanager class by default. To implement dual-buffering by managing bufferedgraphicscontext, follow these steps:

(1) Obtain a reference to the instance of the bufferedgraphicscontext class.

(2) Call bufferedgraphicscontext. Allocate to create a bufferedgraphics class instance.

(3) Draw a graph to the graph buffer by setting the bufferedgraphics. Graphics attribute.

(4) When drawing in all the graphic buffers is completed, you can call the bufferedgraphics. Render method to display the buffer content to the drawing associated with the buffer or the specified drawing.

(5) Call the dispose method for releasing system resources on the bufferedgraphics instance after rendering the image.

In the complete example, 400 random circles are drawn in a rectangle of 400*10000.

Bufferedgraphicscontext current = bufferedgraphicsmanager. current; // (1) </P> <p> bufferedgraphics BG; </P> <p> BG = current. allocate (this. creategraphics (), this. displayrectangle); // (2) </P> <p> graphics G = BG. graphics; // (3) </P> <p> // random width 400 high 400 </P> <p> system. random RND = new random (); </P> <p> int X, Y, W, H, R, I; </P> <p> for (I = 0; I <10000; I ++) </P> <p >{</P> <p> X = RND. next (400); </P> <p> Y = RND. next (400); </P> <p> r = RND. next (20); </P> <p> W = RND. next (10); </P> <p> H = RND. next (10); </P> <p> G. drawellipse (pens. blue, X, Y, W, H); </P> <p >}</P> <p> BG. render (); // (4) </P> <p>/BG. render (this. creategraphics (); </P> <p> BG. dispose (); // (5) <br/>

3. Open a buffer (for example, a non-displayed bitmap object), and display it once after the painting is complete.

The complete code is as follows:

Bitmap bt = new Bitmap (400,400); </P> <p> graphics BG = graphics. fromimage (BT); </P> <p> system. random RND = new random (); </P> <p> int X, Y, W, H, R, I; </P> <p> for (I = 0; I <10000; I ++) </P> <p >{</P> <p> X = RND. next (400); </P> <p> Y = RND. next (400); </P> <p> r = RND. next (20); </P> <p> W = RND. next (10); </P> <p> H = RND. next (10); </P> <p> BG. drawellipse (pens. blue, X, Y, W, H); </P> <p >}</P> <p> This. creategraphics (). drawimage (BT, new point (0, 0); <br/>

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.