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 Forms 搭配使用所設計,而且需要 PaintEventArgse,這是 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:

  • 從範例目錄中的 SampImag 圖形檔案建立 ImageCreates an Image from a graphics file SampImag.jpg in the example directory.

  • Image建立 GraphicsCreates a Graphics from the Image.

  • 藉由在其中填入矩形來改變影像。Alters the image by filling a rectangle within it.

  • Image 繪製到螢幕上。Draws the Image to the screen.

  • 釋放建立的 GraphicsReleases 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.