This documentation is archived and is not being maintained.

Pen Constructor (Brush, Single)

Initializes a new instance of the Pen class with the specified Brush and Width.

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

public Pen(
	Brush brush,
	float width
)

Parameters

brush
Type: System.Drawing.Brush
A Brush that determines the characteristics of this Pen.
width
Type: System.Single
The width of the new Pen.

ExceptionCondition
ArgumentNullException

brush is null.

The Brush is set to the color specified in the brush parameter, the Width property is set to the value specified in the width parameter, and the units are set to World.

Note that the brush parameter also specifies the Color property of this Pen.

A width of 0 will result in the Pen drawing as if the width were 1.

The following code example creates a Pen and demonstrates the effects of setting the StartCap and EndCap properties on a Pen.

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


private void Button3_Click(System.Object sender, System.EventArgs e)
{

    Graphics buttonGraphics = Button3.CreateGraphics();
    Pen myPen = new Pen(Color.ForestGreen, 4.0F);
    myPen.DashStyle = System.Drawing.Drawing2D.DashStyle.DashDotDot;

    Rectangle theRectangle = Button3.ClientRectangle;
    theRectangle.Inflate(-2, -2);
    buttonGraphics.DrawRectangle(myPen, theRectangle);
    buttonGraphics.Dispose();
    myPen.Dispose();
}


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: