次の方法で共有


方法: バッファリングされたグラフィックスを手動でレンダリングする

バッファーされた独自のグラフィックスを管理する場合は、グラフィックス バッファーを作成してレンダリングできる必要があります。 BufferedGraphics メソッドを呼び出すことで、画面上の描画サーフェイスに関連付けられているAllocate クラスのインスタンスを作成できます。 このメソッドは、フォームやコントロールなど、特定のレンダリング サーフェイスに関連付けられた BufferedGraphics インスタンスを作成します。 BufferedGraphics インスタンスを作成したら、Graphics プロパティを使用して、そのインスタンスが表すバッファーにグラフィックスを描画できます。 すべてのグラフィックス操作を実行したら、 Render メソッドを呼び出してバッファーの内容を画面にコピーできます。

独自のレンダリングを実行する場合、メモリ消費量は増加しますが、増加はわずかな場合があります。

バッファリングされたグラフィックスを手動で表示するには

  1. BufferedGraphicsContext クラスのインスタンスへの参照を取得します。 詳細については、「 方法: バッファリングされたグラフィックスを手動で管理する」を参照してください。

  2. 次のコード例に示すように、BufferedGraphics メソッドを呼び出して、Allocate クラスのインスタンスを作成します。

    // 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. グラフィックスのレンダリングが完了したら、Dispose インスタンスで BufferedGraphics メソッドを呼び出して、システム リソースを解放します。

    myBuffer.Dispose();
    
    myBuffer.Dispose()
    

こちらも参照ください