如果要管理自己的缓冲图形,则需要能够创建和呈现图形缓冲区。 通过调用Allocate方法,可以创建与屏幕上绘图图面关联的BufferedGraphics类的实例。 此方法创建与 BufferedGraphics 特定呈现图面(如窗体或控件)关联的实例。 创建 BufferedGraphics 实例后,可以将图形绘制到它通过属性表示的 Graphics 缓冲区。 执行所有图形作后,可以通过调用 Render 该方法将缓冲区的内容复制到屏幕。
注释
如果执行自己的渲染,则内存消耗将增加,但增加可能只是轻微的。
手动显示缓冲图形
获取对 BufferedGraphicsContext 类实例的引用。 有关详细信息,请参阅 如何:手动管理缓冲图形。
通过调用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)
通过设置 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)
完成图形缓冲区的所有绘图操作后,调用 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)
完成图形呈现后,在 BufferedGraphics 实例上调用
Dispose
方法以释放系统资源。myBuffer.Dispose();
myBuffer.Dispose()