Edit

Share via


Pen Constructors

Definition

Initializes a new instance of the Pen class with the specified color.

Overloads

Pen(Brush)

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

Pen(Color)

Initializes a new instance of the Pen class with the specified color.

Pen(Brush, Single)

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

Pen(Color, Single)

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

Pen(Brush)

Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs

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

C#
public Pen(System.Drawing.Brush brush);

Parameters

brush
Brush

A Brush that determines the fill properties of this Pen.

Exceptions

brush is null.

Examples

The following code example demonstrates constructing a Pen with a Brush and the effects of setting the LineJoin property on 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.

C#
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();
}

Remarks

The Brush property determines how the Pen draws lines. Lines are drawn as if they are filled rectangles, with the characteristics of the specified Brush.

The Width property of the new Pen is set to 1 (the default).

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Pen(Color)

Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs

Initializes a new instance of the Pen class with the specified color.

C#
public Pen(System.Drawing.Color color);

Parameters

color
Color

A Color structure that indicates the color of this Pen.

Remarks

The Color property is set to the color specified by the color parameter. The Width property is set to 1 (the default).

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Pen(Brush, Single)

Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs

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

C#
public Pen(System.Drawing.Brush brush, float width);

Parameters

brush
Brush

A Brush that determines the characteristics of this Pen.

width
Single

The width of the new Pen.

Exceptions

brush is null.

Examples

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.

C#
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();
}

Remarks

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.

If this value is 0, the width in device units is always 1 pixel— it is not affected by scale-transform operations that are in effect for the Graphics object that the Pen is used for.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

Pen(Color, Single)

Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs
Source:
Pen.cs

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

C#
public Pen(System.Drawing.Color color, float width);

Parameters

color
Color

A Color structure that indicates the color of this Pen.

width
Single

A value indicating the width of this Pen.

Examples

The following code example demonstrates creating a Pen and the effects of setting the DashCap, DashPattern, and SmoothingMode properties.

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

C#
private void ShowPensAndSmoothingMode(PaintEventArgs e)
{

    // Set the SmoothingMode property to smooth the line.
    e.Graphics.SmoothingMode = 
        System.Drawing.Drawing2D.SmoothingMode.AntiAlias;

    // Create a new Pen object.
    Pen greenPen = new Pen(Color.Green);

    // Set the width to 6.
    greenPen.Width = 6.0F;

    // Set the DashCap to round.
    greenPen.DashCap = System.Drawing.Drawing2D.DashCap.Round;

    // Create a custom dash pattern.
    greenPen.DashPattern = new float[]{4.0F, 2.0F, 1.0F, 3.0F};

    // Draw a line.
    e.Graphics.DrawLine(greenPen, 20.0F, 20.0F, 100.0F, 240.0F);

    // Change the SmoothingMode to none.
    e.Graphics.SmoothingMode = 
        System.Drawing.Drawing2D.SmoothingMode.None;

    // Draw another line.
    e.Graphics.DrawLine(greenPen, 100.0F, 240.0F, 160.0F, 20.0F);

    // Dispose of the custom pen.
    greenPen.Dispose();
}

Remarks

The Color property is set to the color specified by the color parameter. The Width property is set to the value specified in the width parameter. If this value is 0, the width in device units is always 1 pixel— it is not affected by scale-transform operations that are in effect for the Graphics object that the Pen is used for.

Applies to

.NET 10 (package-provided) and other versions
Product Versions
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10