如何:手动呈现缓冲图形

更新:2007 年 11 月

若要管理自己的缓冲图形,需要能够创建图形缓冲区并呈现其内容。通过调用 Allocate 可以创建与屏幕上的绘图图面关联的 BufferedGraphics 类的实例。此方法创建一个与特定呈现图面(如窗体或控件)关联的 BufferedGraphics 实例。创建 BufferedGraphics 实例后,可以将图形绘制到由该实例的 Graphics 属性表示的缓冲区。执行所有图形操作后,可通过调用 Render 方法将缓冲区的内容复制到屏幕上。

说明:

如果执行自己的呈现,内存消耗将会增加,但可能只是微量增加。

手动显示缓冲的图形

  1. 获得对 BufferedGraphicsContext 类的实例的引用。有关更多信息,请参见 如何:手动管理缓冲图形

  2. 通过调用 Allocate 方法来创建 BufferedGraphics 类的一个实例,如下面的代码示例所示。

    ' 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)
    
    
    // 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);
    
  3. 通过设置 Graphics 属性将图形绘制到图形缓冲区。例如:

    ' Draws an ellipse to the graphics buffer.
    myBuffer.Graphics.DrawEllipse(Pens.Blue, Me.DisplayRectangle)
    
    // Draws an ellipse to the graphics buffer.
    myBuffer.Graphics.DrawEllipse(Pens.Blue, this.DisplayRectangle);
    
  4. 当完成所有图形缓冲区中的绘制操作时,可调用 Render 方法将缓冲区的内容呈现到与该缓冲区关联的绘图图面或者指定的绘图图面,如下面的代码示例所示。

    ' 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)
    
    // 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());
    
  5. 完成呈现图形之后,对 BufferedGraphics 实例调用释放系统资源的 Dispose 方法。

    myBuffer.Dispose()
    
    myBuffer.Dispose();
    

请参见

任务

如何:手动管理缓冲图形

概念

双缓冲图形

参考

BufferedGraphicsContext

BufferedGraphics