Dispose Method
Collapse the table of content
Expand the table of content

Pen.Dispose Method ()

 

Releases all resources used by this Pen.

Namespace:   System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)

public void Dispose()

Calling Dispose allows the resources used by this Brush to be reallocated for other purposes.

Call Dispose when you are finished using the Pen. The Dispose method leaves the Pen in an unusable state. After calling Dispose, you must release all references to the Pen so the garbage collector can reclaim the memory that the Pen was occupying. For more information, see Cleaning Up Unmanaged Resources and Implementing a Dispose Method.

System_CAPS_noteNote

Always call Dispose before you release your last reference to the Pen. Otherwise, the resources it is using will not be freed until the garbage collector calls the Pen object's Finalize method.

The following code example demonstrates the effects of setting the Width and LineJoin properties, and shows how to call the Dispose method for a Pen.

This example is designed to be used with Windows Forms. Paste the code into a form and call the ShowLineJoin method when handling the form's Paint event, passing e as PaintEventArgs.

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();

}

.NET Framework
Available since 1.1
Return to top
Show:
© 2016 Microsoft