Graphics.Dispose 方法

定義

釋放這個 Graphics 所使用的所有資源。

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

實作

範例

下列程式碼範例是設計來搭配Windows Forms使用,而且需要 PaintEventArgse ,這是事件處理常式的參數 Paint 。 此程式碼會執行下列動作:

  • Image從範例目錄中的圖形檔案SampImag.jpg建立 。

  • GraphicsImage 建立 。

  • 藉由在影像中填滿矩形來改變影像。

  • Image將 繪製到畫面。

  • 釋放建立 Graphics 的 。

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

備註

呼叫 Dispose 可讓這個 Graphics 所使用的資源重新配置以供其他用途使用。

適用於