Pen Class
The Pen class defines an object used to draw lines and curves. It encapsulates a Brush, width, dash style, line caps, and more.
Namespace
System.Drawing.Drawing2D
Inheritance
Object → Pen
Constructor Summary
| Signature | Description |
|---|---|
Pen(Color color) | Creates a new pen with the specified color and a width of 1.0. |
Pen(Color color, float width) | Creates a new pen with the specified color and width. |
Pen(Brush brush, float width) | Creates a new pen that uses the specified brush and width. |
Properties
| Name | Type | Description |
|---|---|---|
Color | Color | Gets or sets the color of the pen. |
Width | float | Gets or sets the width of the pen. |
DashStyle | DashStyle | Specifies the dash pattern. |
StartCap | LineCap | Defines the shape of the line's start cap. |
EndCap | LineCap | Defines the shape of the line's end cap. |
LineJoin | LineJoin | Specifies the type of join for two lines. |
MiterLimit | float | Limits the length of the miter. |
Methods
| Signature | Description |
|---|---|
Clone() | Creates an exact copy of the Pen. |
Dispose() | Releases all resources used by the Pen. |
ResetTransform() | Resets the transformation matrix to identity. |
MultiplyTransform(Matrix matrix, MatrixOrder order = MatrixOrder.Prepend) | Multiplies the current transformation matrix. |
Example
// Draw a dashed red line using Pen
using System.Drawing;
using System.Drawing.Drawing2D;
public void DrawSample(Graphics g)
{
using (Pen pen = new Pen(Color.Red, 3))
{
pen.DashStyle = DashStyle.Dash;
pen.StartCap = LineCap.Round;
pen.EndCap = LineCap.ArrowAnchor;
g.DrawLine(pen, new Point(20, 20), new Point(200, 150));
}
}