Bagikan melalui


Double Buffered Graphics

Flicker is a common problem when programming graphics. Graphics operations that require multiple complex painting operations can cause the rendered images to appear to flicker or have an otherwise unacceptable appearance. To address these problems, the .NET Framework provides access to double buffering.

Double buffering uses a memory buffer to address the flicker problems associated with multiple paint operations. When double buffering is enabled, all paint operations are first rendered to a memory buffer instead of the drawing surface on the screen. After all paint operations are completed, the memory buffer is copied directly to the drawing surface associated with it. Because only one graphics operation is performed on the screen, the image flickering associated with complex painting operations is eliminated.

Default Double Buffering

The easiest way to use double buffering in your applications is to use the default double buffering for forms and controls that is provided by the .NET Framework. You can enable default double buffering for your Windows Forms and authored Windows controls by setting the DoubleBuffered property to true or by using the SetStyle method. For more information, see How to: Reduce Graphics Flicker with Double Buffering for Forms and Controls.

Manually Managing Buffered Graphics

For more advanced double buffering scenarios, such as animation or advanced memory management, you can use the .NET Framework classes to implement your own double-buffering logic. The class responsible for allocating and managing individual graphics buffers is the BufferedGraphicsContext class. Every application domain has its own default BufferedGraphicsContext instance that manages all of the default double buffering for that application. In most cases there will be only one application domain per application, so there is generally one default BufferedGraphicsContext per application. Default BufferedGraphicsContext instances are managed by the BufferedGraphicsManager class. You can retrieve a reference to the default BufferedGraphicsContext instance by calling the BufferedGraphicsManager.Current Property. You can also create a dedicated BufferedGraphicsContext instance, which can improve performance for graphically intensive applications. For information on how to create a BufferedGraphicsContext instance, see How to: Manually Manage Buffered Graphics.

Manually Displaying Buffered Graphics

You can use an instance of the BufferedGraphicsContext class to create graphics buffers by calling the BufferedGraphicsContext.Allocate Method, which returns an instance of the BufferedGraphics class. A BufferedGraphics object manages a memory buffer that is associated with a rendering surface, such as a form or control.

After it is instantiated, the BufferedGraphics class manages rendering to an in-memory graphics buffer. You can render graphics to the memory buffer through the BufferedGraphics.Graphics Property, which exposes a Graphics object that directly represents the memory buffer. You can paint to this Graphics object just as you would to a Graphics object that represents a drawing surface. After all the graphics have been drawn to the buffer, you can use the BufferedGraphics.Render Method to copy the contents of the buffer to the drawing surface on the screen.

For more information on using the BufferedGraphics class, see Manually Rendering Buffered Graphics. For more information on rendering graphics, see Graphics and Drawing in Windows Forms

See Also

Tasks

How to: Manually Render Buffered Graphics

How to: Reduce Graphics Flicker with Double Buffering for Forms and Controls

How to: Manually Manage Buffered Graphics

Reference

BufferedGraphics

BufferedGraphicsContext

BufferedGraphicsManager

Other Resources

Graphics and Drawing in Windows Forms