Pen Constructor (Brush, Single)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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


Type: System.Drawing.Brush

A Brush that determines the characteristics of this Pen.

Type: System.Single

The width of the new Pen.

Exception Condition

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

.NET Framework
Available since 1.1
Return to top