雙重緩衝的圖形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. 為了解決這些問題,.NET Framework 提供雙重緩衝的存取。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

在應用程式中使用雙重緩衝的最簡單方式,是使用 .NET Framework 所提供之表單和控制項的預設雙重緩衝。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. 您可以啟用預設雙重緩衝的 Windows Form,並藉由設定撰寫 Windows 控制項DoubleBuffered屬性,以true或使用SetStyle方法。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

若是動畫或進階記憶體管理這類更進階雙重緩衝案例,您可以使用 .NET Framework 類別來實作您自己的雙重緩衝邏輯。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. 負責配置和管理個別圖形緩衝區的類別是BufferedGraphicsContext類別。The class responsible for allocating and managing individual graphics buffers is the BufferedGraphicsContext class. 每個應用程式網域有它自己的預設值BufferedGraphicsContext管理所有預設雙重緩衝該應用程式的執行個體。Every application domain has its own default BufferedGraphicsContext instance that manages all of the default double buffering for that application. 在大部分情況下會有一個應用程式定義域,每個應用程式,因此通常是一個預設BufferedGraphicsContext每個應用程式。In most cases there will be only one application domain per application, so there is generally one default BufferedGraphicsContext per application. 預設值BufferedGraphicsContext執行個體由BufferedGraphicsManager類別。Default BufferedGraphicsContext instances are managed by the BufferedGraphicsManager class. 您可以擷取預設值的參考BufferedGraphicsContext藉由呼叫的執行個體CurrentYou can retrieve a reference to the default BufferedGraphicsContext instance by calling the Current. 您也可以建立專用BufferedGraphicsContext執行個體,以改善大量繪圖應用程式的效能。You can also create a dedicated BufferedGraphicsContext instance, which can improve performance for graphically intensive applications. 如需如何建立BufferedGraphicsContext執行個體,請參閱How to:手動管理已緩衝的圖形For information on how to create a BufferedGraphicsContext instance, see How to: Manually Manage Buffered Graphics.

手動顯示已緩衝的圖形Manually Displaying Buffered Graphics

您可以使用的執行個體BufferedGraphicsContext類別來建立圖形緩衝區,藉由呼叫BufferedGraphicsContext.Allocate,它會傳回的執行個體BufferedGraphics類別。You can use an instance of the BufferedGraphicsContext class to create graphics buffers by calling the BufferedGraphicsContext.Allocate, which returns an instance of the BufferedGraphics class. ABufferedGraphics物件會管理轉譯介面,例如表單或控制項相關聯的記憶體緩衝區。A BufferedGraphics object manages a memory buffer that is associated with a rendering surface, such as a form or control.

它會具現化之後,BufferedGraphics類別會管理轉譯到記憶體內圖形緩衝區。After it is instantiated, the BufferedGraphics class manages rendering to an in-memory graphics buffer. 您可以將圖形轉譯到記憶體緩衝區,透過Graphics,以公開Graphics直接代表記憶體緩衝區的物件。You can render graphics to the memory buffer through the Graphics, which exposes a Graphics object that directly represents the memory buffer. 您可以繪製至這個Graphics物件,就像您一樣以Graphics物件,表示繪圖介面。You can paint to this Graphics object just as you would to a Graphics object that represents a drawing surface. 所有圖形都繪製到緩衝區之後,您可以使用BufferedGraphics.Render緩衝區的內容複製到螢幕上的繪圖介面。After all the graphics have been drawn to the buffer, you can use the BufferedGraphics.Render to copy the contents of the buffer to the drawing surface on the screen.

如需有關使用BufferedGraphics類別,請參閱手動轉譯已緩衝的圖形For more information on using the BufferedGraphics class, see Manually Rendering Buffered Graphics. 如需轉譯圖形的詳細資訊,請參閱 Windows Forms 中的圖形和繪圖For more information on rendering graphics, see Graphics and Drawing in Windows Forms

另請參閱See also