语言

Pen.Dispose 方法

定义

释放由此 Pen使用的所有资源。

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

实现

示例

下面的代码示例演示设置 Width 和 LineJoin 属性的效果,并演示如何调用 Dispose 方法 Pen。

此示例旨在与 Windows 窗体一起使用。 将代码粘贴到窗体中,并在处理表单的事件时调用ShowLineJoin该方法,并作为传递方式Paint传递e。PaintEventArgs

private:
   void ShowLineJoin( PaintEventArgs^ e )
   {
      // Create a new pen.
      Pen^ skyBluePen = gcnew Pen( Brushes::DeepSkyBlue );

      // Set the pen's width.
      skyBluePen->Width = 8.0F;

      // Set the LineJoin property.
      skyBluePen->LineJoin = System::Drawing::Drawing2D::LineJoin::Bevel;

      // Draw a rectangle.
      e->Graphics->DrawRectangle( skyBluePen, Rectangle(40,40,150,200) );

      //Dispose of the pen.
      delete skyBluePen;
   }
private void ShowLineJoin(PaintEventArgs e)
{

    // Create a new pen.
    Pen skyBluePen = new Pen(Brushes.DeepSkyBlue);

    // Set the pen's width.
    skyBluePen.Width = 8.0F;

    // Set the LineJoin property.
    skyBluePen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;

    // Draw a rectangle.
    e.Graphics.DrawRectangle(skyBluePen, 
        new Rectangle(40, 40, 150, 200));

    //Dispose of the pen.
    skyBluePen.Dispose();
}
Private Sub ShowLineJoin(ByVal e As PaintEventArgs)

    ' Create a new pen.
    Dim skyBluePen As New Pen(Brushes.DeepSkyBlue)

    ' Set the pen's width.
    skyBluePen.Width = 8.0F

    ' Set the LineJoin property.
    skyBluePen.LineJoin = Drawing2D.LineJoin.Bevel

    ' Draw a rectangle.
    e.Graphics.DrawRectangle(skyBluePen, _
        New Rectangle(40, 40, 150, 200))

    'Dispose of the pen.
    skyBluePen.Dispose()

End Sub

注解

调用 Dispose 允许将由此 Brush 使用的资源重新分配为其他目的。

使用Dispose完时调用 Pen 。 该方法 Dispose 保持 Pen 不可用状态。 调用 Dispose后,必须释放对 Pen 垃圾回收器的所有引用,以便垃圾回收器可以回收占用的 Pen 内存。 有关详细信息,请参阅 清理非托管资源 并 实现 Dispose 方法。

注释

在发布对 .. 的Dispose上次引用之前,请始终调用Pen。 否则,在垃圾回收器调用 Pen 对象的 Finalize 方法之前,不会释放正在使用的资源。

适用于