如何:手动呈现缓冲图形

如果你正在管理自己的缓冲图形,你将需要能创建和程序图形缓冲区。 你可以通过调用 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()
    

另请参阅