作法:手動呈現已緩衝的圖形

如果您在管理自己的已緩衝圖形,將需要能夠建立及呈現圖形緩衝區。 您可以藉由呼叫類別 Allocate 方法,針對與螢幕上繪圖介面相關聯的 BufferedGraphics 類別建立其執行個體。 這個方法會建立與特定轉譯介面 (例如表單或控制項) 相關聯之 BufferedGraphics 的執行個體。 建立 BufferedGraphics 執行個體之後,您可以透過 Graphics 屬性,繪製圖形到它所代表的緩衝區。 在您執行所有圖形作業之後,可以藉由呼叫 Render 方法,將緩衝區的內容複製到螢幕上。

注意

如果您執行您自己的轉譯,將會增加記憶體耗用量,不過可能只會稍微增加。

手動顯示已緩衝的圖形

  1. 取得 BufferedGraphicsContext 類別執行個體的參考。 如需詳細資訊,請參閱 如何:手動管理緩衝圖形

  2. 藉由呼叫 Allocate 方法,建立 BufferedGraphics 類別的執行個體,如下列程式碼範例所示。

    // 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 屬性,繪製圖形至圖形緩衝區。 例如:

    // 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 方法以轉譯緩衝區,不論是轉譯到與該緩衝區關聯的繪圖介面,或是指定的繪圖介面,如下列程式碼範例所示。

    // 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 方法以釋放系統資源。

    myBuffer.Dispose();
    
    myBuffer.Dispose()
    

另請參閱