HOW TO:手動呈現已緩衝的圖形How to: Manually Render Buffered Graphics

如果您在管理自己的已緩衝圖形,將需要能夠建立及呈現圖形緩衝區。If you are managing your own buffered graphics, you will need to be able to create and render graphics buffers. 您可以藉由呼叫類別 Allocate 方法,針對與螢幕上繪圖介面相關聯的 BufferedGraphics 類別建立其執行個體。You can create instances of the BufferedGraphics class that is associated with drawing surfaces on your screen by calling the Allocate method. 這個方法會建立與特定轉譯介面 (例如表單或控制項) 相關聯之 BufferedGraphics 的執行個體。This method creates a BufferedGraphics instance that is associated with a particular rendering surface, such as a form or control. 建立 BufferedGraphics 執行個體之後,您可以透過 Graphics 屬性,繪製圖形到它所代表的緩衝區。After you have created a BufferedGraphics instance, you can draw graphics to the buffer it represents through the Graphics property. 在您執行所有圖形作業之後,可以藉由呼叫 Render 方法,將緩衝區的內容複製到螢幕上。After you have performed all graphics operations, you can copy the contents of the buffer to the screen by calling the Render method.

注意

如果您執行您自己的轉譯,將會增加記憶體耗用量,不過可能只會稍微增加。If you perform your own rendering, memory consumption will increase, though the increase may only be slight.

手動顯示已緩衝的圖形To manually display buffered graphics

  1. 取得 BufferedGraphicsContext 類別執行個體的參考。Obtain a reference to an instance of the BufferedGraphicsContext class. 如需詳細資訊,請參閱如何:手動管理已緩衝的圖形For more information, see How to: Manually Manage Buffered Graphics.

  2. 藉由呼叫 Allocate 方法,建立 BufferedGraphics 類別的執行個體,如下列程式碼範例所示。Create an instance of the BufferedGraphics class by calling the Allocate method, as shown in the following code example.

    // This example assumes the existence of a form called Form1.
    BufferedGraphicsContext currentContext;
    BufferedGraphics myBuffer;
    // Gets a reference to the current BufferedGraphicsContext
    currentContext = BufferedGraphicsManager.Current;
    // Creates a BufferedGraphics instance associated with Form1, and with 
    // dimensions the same size as the drawing surface of Form1.
    myBuffer = currentContext.Allocate(this.CreateGraphics(),
       this.DisplayRectangle);
    
    ' This example assumes the existence of a form called Form1.
    Dim currentContext As BufferedGraphicsContext
    Dim myBuffer As BufferedGraphics
    ' Gets a reference to the current BufferedGraphicsContext.
    currentContext = BufferedGraphicsManager.Current
    ' Creates a BufferedGraphics instance associated with Form1, and with 
    ' dimensions the same size as the drawing surface of Form1.
    myBuffer = currentContext.Allocate(Me.CreateGraphics, _
       Me.DisplayRectangle)
    
    
  3. 藉由設定 Graphics 屬性,繪製圖形至圖形緩衝區。Draw graphics to the graphics buffer by setting the Graphics property. 例如:For example:

    // Draws an ellipse to the graphics buffer.
    myBuffer.Graphics.DrawEllipse(Pens.Blue, this.DisplayRectangle);
    
    ' Draws an ellipse to the graphics buffer.
    myBuffer.Graphics.DrawEllipse(Pens.Blue, Me.DisplayRectangle)
    
  4. 當您完成對圖形緩衝區的所有繪圖作業時,請呼叫 Render 方法以轉譯緩衝區,不論是轉譯到與該緩衝區關聯的繪圖介面,或是指定的繪圖介面,如下列程式碼範例所示。When you have completed all of your drawing operations to the graphics buffer, call the Render method to render the buffer, either to the drawing surface associated with that buffer, or to a specified drawing surface, as shown in the following code example.

    // This example assumes the existence of a BufferedGraphics instance
    // called myBuffer.
    // Renders the contents of the buffer to the drawing surface associated 
    // with the buffer.
    myBuffer.Render();
    // Renders the contents of the buffer to the specified drawing surface.
    myBuffer.Render(this.CreateGraphics());
    
    ' Renders the contents of the buffer to the drawing surface associated 
    ' with the buffer.
    myBuffer.Render()
    ' Renders the contents of the buffer to the specified drawing surface.
    myBuffer.Render(Me.CreateGraphics)
    
  5. 完成轉譯圖形之後,請對 BufferedGraphics 執行個體呼叫 Dispose 方法以釋放系統資源。After you are finished rendering graphics, call the Dispose method on the BufferedGraphics instance to free system resources.

    myBuffer.Dispose();
    
    myBuffer.Dispose()
    

另請參閱See also