Graphics.Dispose 方法

释放由 Graphics 使用的所有资源。

**命名空间:**System.Drawing
**程序集:**System.Drawing(在 system.drawing.dll 中)

语法

声明
Public Sub Dispose
用法
Dim instance As Graphics

instance.Dispose
public void Dispose ()
public:
virtual void Dispose () sealed
public final void Dispose ()
public final function Dispose ()

返回值

此方法不返回值。

备注

调用 Dispose 使您可以为其他目的而重新分配此 Graphics 所使用的资源。

示例

下面的代码示例设计为与 Windows 窗体一起使用,它需要 PaintEventArgse,即 Paint 事件处理程序的一个参数。代码执行下列操作:

  • 从示例目录中的图形文件 SampImag.jpg 创建 Image

  • Image 创建 Graphics

  • 通过填充图像中的矩形来改变该图像。

  • Image 绘制到屏幕。

  • 释放创建的 Graphics

Public 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.
    newGraphics.Dispose()
End Sub
public 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.
    newGraphics.Dispose();
}
public:
   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;
   }

平台

Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

Graphics 类
Graphics 成员
System.Drawing 命名空间