Pen.DashCap Property

 

Gets or sets the cap style used at the end of the dashes that make up dashed lines drawn with this Pen.

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

public DashCap DashCap { get; set; }

Property Value

Type: System.Drawing.Drawing2D.DashCap

One of the DashCap values that represents the cap style used at the beginning and end of the dashes that make up dashed lines drawn with this Pen.

Exception Condition
InvalidEnumArgumentException

The specified value is not a member of DashCap.

ArgumentException

The DashCap property is set on an immutable Pen, such as those returned by the Pens class.

Do not set this property to Triangle if the Pen has its Alignment property set to Inset.

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.

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

.NET Framework
Available since 1.1
Return to top
Show: