Graphics.Dispose 方法


释放此 Graphics 使用的所有资源。Releases all resources used by this Graphics.

 virtual void Dispose();
public void Dispose ();
abstract member Dispose : unit -> unit
override this.Dispose : unit -> unit
Public Sub Dispose ()



下面的代码示例旨在与 Windows 窗体一起使用,并且它需要PaintEventArgs e作为Paint事件处理程序的参数。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 该代码执行下列操作:The code performs the following actions:

  • 在示例Image目录中从图形文件 SampImag 创建一个。Creates an Image from a graphics file SampImag.jpg in the example directory.

  • 从创建Graphics一个。 ImageCreates a Graphics from the Image.

  • 通过填充图像中的矩形来更改图像。Alters the image by filling a rectangle within it.

  • 将绘制Image到屏幕。Draws the Image to the screen.

  • 释放创建Graphics的。Releases the created Graphics.

   void FromImageImage1( PaintEventArgs^ e )
      // Create image.
      Image^ imageFile = Image::FromFile( "SampImag.jpg" );

      // Create graphics object for alteration.
      Graphics^ newGraphics = Graphics::FromImage( imageFile );

      // Alter image.
      newGraphics->FillRectangle( gcnew SolidBrush( Color::Black ), 100, 50, 100, 100 );

      // Draw image to screen.
      e->Graphics->DrawImage( imageFile, PointF(0.0F,0.0F) );

      // Release graphics object.
      delete newGraphics;
private void FromImageImage1(PaintEventArgs e)
    // Create image.
    Image imageFile = Image.FromFile("SampImag.jpg");
    // Create graphics object for alteration.
    Graphics newGraphics = Graphics.FromImage(imageFile);
    // Alter image.
    newGraphics.FillRectangle(new SolidBrush(Color.Black), 100, 50, 100, 100);
    // Draw image to screen.
    e.Graphics.DrawImage(imageFile, new PointF(0.0F, 0.0F));
    // Release graphics object.
Private Sub FromImageImage1(ByVal e As PaintEventArgs)

    ' Create image.
    Dim imageFile As Image = Image.FromFile("SampImag.jpg")

    ' Create graphics object for alteration.
    Dim newGraphics As Graphics = Graphics.FromImage(imageFile)

    ' Alter image.
    newGraphics.FillRectangle(New SolidBrush(Color.Black), 100, _
    50, 100, 100)

    ' Draw image to screen.
    e.Graphics.DrawImage(imageFile, New PointF(0.0F, 0.0F))

    ' Dispose of graphics object.
End Sub


调用Dispose会允许出于其他目的重新Graphics分配此所使用的资源。Calling Dispose allows the resources used by this Graphics to be reallocated for other purposes.