Class Pen

Class Pen

Namespace: Aspose.Imaging
Assembly: Aspose.Imaging.dll (25.2.0)

Defines an object used to draw lines, curves and figures.

public class Pen : TransparencySupporter

Inheritance

objectTransparencySupporterPen

Inherited Members

TransparencySupporter.Equals(object), TransparencySupporter.GetHashCode(), TransparencySupporter.Equals(TransparencySupporter), TransparencySupporter.Opacity, object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Examples

This example shows the creation and usage Pen objects. The example creates a new Image and draw Rectangles on Image surface.```csharp [C#]

                                                                                                                                   //Create an instance of BmpOptions and set its various properties
                                                                                                                                   Aspose.Imaging.ImageOptions.BmpOptions bmpOptions = new Aspose.Imaging.ImageOptions.BmpOptions();
                                                                                                                                   bmpOptions.BitsPerPixel = 24;

                                                                                                                                   //Create an instance of FileCreateSource and assign it as Source for the instance of BmpOptions
                                                                                                                                   //Second Boolean parameter determines if the file to be created IsTemporal or not
                                                                                                                                   bmpOptions.Source = new Aspose.Imaging.Sources.FileCreateSource(@"C:\temp\sample.bmp", false);

                                                                                                                                   //Create an instance of Image at specified Path
                                                                                                                                   using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Create(bmpOptions, 500, 500))
                                                                                                                                   {
                                                                                                                                       //Create an instance of Graphics and initialize it with Image object
                                                                                                                                       Aspose.Imaging.Graphics graphics = new Aspose.Imaging.Graphics(image);

                                                                                                                                       //Clear the Graphics sutface with White Color
                                                                                                                                       graphics.Clear(Aspose.Imaging.Color.White);

                                                                                                                                       //Create an instance of Pen with color Red and width 5
                                                                                                                                       Aspose.Imaging.Pen pen = new Aspose.Imaging.Pen(Aspose.Imaging.Color.Red, 5f);

                                                                                                                                       //Create an instance of HatchBrush and set its properties
                                                                                                                                       Aspose.Imaging.Brushes.HatchBrush brush = new Aspose.Imaging.Brushes.HatchBrush();
                                                                                                                                       brush.BackgroundColor = Aspose.Imaging.Color.Wheat;
                                                                                                                                       brush.ForegroundColor = Aspose.Imaging.Color.Red;

                                                                                                                                       //Create an instance of Pen
                                                                                                                                       //initialize it with HatchBrush object and width
                                                                                                                                       Aspose.Imaging.Pen brusedpen = new Pen(brush, 5);

                                                                                                                                       //Draw Rectangles by specifying Pen object
                                                                                                                                       graphics.DrawRectangles(pen, new[]
                                                                                                                                       {
                                                                                                                                           new Aspose.Imaging.Rectangle(new Aspose.Imaging.Point(210, 210), new Aspose.Imaging.Size(100, 100)),
                                                                                                                                           new Aspose.Imaging.Rectangle(new Aspose.Imaging.Point(110, 110), new Aspose.Imaging.Size(100, 100)),
                                                                                                                                           new Aspose.Imaging.Rectangle(new Aspose.Imaging.Point(310, 310), new Aspose.Imaging.Size(100, 100))
                                                                                                                                       });

                                                                                                                                       //Draw Rectangles by specifying Pen object
                                                                                                                                       graphics.DrawRectangles(brusedpen, new[]
                                                                                                                                       {
                                                                                                                                           new Aspose.Imaging.Rectangle(new Aspose.Imaging.Point(310, 110), new Aspose.Imaging.Size(100, 100)),
                                                                                                                                           new Aspose.Imaging.Rectangle(new Aspose.Imaging.Point(110, 310), new Aspose.Imaging.Size(100, 100))
                                                                                                                                       });

                                                                                                                                       // save all changes.
                                                                                                                                       image.Save();
                                                                                                                                   }

## Constructors

### <a id="Aspose_Imaging_Pen__ctor_Aspose_Imaging_Color_"></a> Pen\(Color\)

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

```csharp
public Pen(Color color)

Parameters

color Color

A Aspose.Imaging.Pen.Color structure that indicates the color of this Aspose.Imaging.Pen.

Pen(Color, float)

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

public Pen(Color color, float width)

Parameters

color Color

A Aspose.Imaging.Pen.Color structure that indicates the color of this Aspose.Imaging.Pen.

width float

A value indicating the width of this Aspose.Imaging.Pen.

Examples

This example shows the creation and usage Pen objects. The example creates a new Image and draw Rectangles on Image surface.```csharp [C#]

                                                                                                                                   //Create an instance of BmpOptions and set its various properties
                                                                                                                                   Aspose.Imaging.ImageOptions.BmpOptions bmpOptions = new Aspose.Imaging.ImageOptions.BmpOptions();
                                                                                                                                   bmpOptions.BitsPerPixel = 24;

                                                                                                                                   //Create an instance of FileCreateSource and assign it as Source for the instance of BmpOptions
                                                                                                                                   //Second Boolean parameter determines if the file to be created IsTemporal or not
                                                                                                                                   bmpOptions.Source = new Aspose.Imaging.Sources.FileCreateSource(@"C:\temp\sample.bmp", false);

                                                                                                                                   //Create an instance of Image at specified Path
                                                                                                                                   using (Aspose.Imaging.Image image = Aspose.Imaging.Image.Create(bmpOptions, 500, 500))
                                                                                                                                   {
                                                                                                                                       //Create an instance of Graphics and initialize it with Image object
                                                                                                                                       Aspose.Imaging.Graphics graphics = new Aspose.Imaging.Graphics(image);

                                                                                                                                       //Clear the Graphics sutface with White Color
                                                                                                                                       graphics.Clear(Aspose.Imaging.Color.White);

                                                                                                                                       //Create an instance of Pen with color Red and width 5
                                                                                                                                       Aspose.Imaging.Pen pen = new Aspose.Imaging.Pen(Aspose.Imaging.Color.Red, 5f);

                                                                                                                                       //Create an instance of HatchBrush and set its properties
                                                                                                                                       Aspose.Imaging.Brushes.HatchBrush brush = new Aspose.Imaging.Brushes.HatchBrush();
                                                                                                                                       brush.BackgroundColor = Aspose.Imaging.Color.Wheat;
                                                                                                                                       brush.ForegroundColor = Aspose.Imaging.Color.Red;

                                                                                                                                       //Create an instance of Pen
                                                                                                                                       //initialize it with HatchBrush object and width
                                                                                                                                       Aspose.Imaging.Pen brusedpen = new Pen(brush, 5);

                                                                                                                                       //Draw Rectangles by specifying Pen object
                                                                                                                                       graphics.DrawRectangles(pen, new[]
                                                                                                                                       {
                                                                                                                                           new Aspose.Imaging.Rectangle(new Aspose.Imaging.Point(210, 210), new Aspose.Imaging.Size(100, 100)),
                                                                                                                                           new Aspose.Imaging.Rectangle(new Aspose.Imaging.Point(110, 110), new Aspose.Imaging.Size(100, 100)),
                                                                                                                                           new Aspose.Imaging.Rectangle(new Aspose.Imaging.Point(310, 310), new Aspose.Imaging.Size(100, 100))
                                                                                                                                       });

                                                                                                                                       //Draw Rectangles by specifying Pen object
                                                                                                                                       graphics.DrawRectangles(brusedpen, new[]
                                                                                                                                       {
                                                                                                                                           new Aspose.Imaging.Rectangle(new Aspose.Imaging.Point(310, 110), new Aspose.Imaging.Size(100, 100)),
                                                                                                                                           new Aspose.Imaging.Rectangle(new Aspose.Imaging.Point(110, 310), new Aspose.Imaging.Size(100, 100))
                                                                                                                                       });

                                                                                                                                       // save all changes.
                                                                                                                                       image.Save();
                                                                                                                                   }

### <a id="Aspose_Imaging_Pen__ctor_Aspose_Imaging_Brush_"></a> Pen\(Brush\)

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

```csharp
public Pen(Brush brush)

Parameters

brush Brush

A Aspose.Imaging.Pen.Brush that determines the fill properties of this Aspose.Imaging.Pen.

Exceptions

ArgumentNullException

brush is null.

Pen(Brush, float)

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

[JsonConstructor]
public Pen(Brush brush, float width)

Parameters

brush Brush

A Aspose.Imaging.Pen.Brush that determines the characteristics of this Aspose.Imaging.Pen.

width float

The width of the new Aspose.Imaging.Pen.

Exceptions

ArgumentNullException

brush is null.

Properties

Alignment

Gets or sets the alignment for this Aspose.Imaging.Pen.

public PenAlignment Alignment { get; set; }

Property Value

PenAlignment

Exceptions

ArgumentException

The Aspose.Imaging.Pen.Alignment property is set on an immutable Aspose.Imaging.Pen, such as those returned by the Aspose.Imaging.Pen class.

Brush

Gets or sets the Aspose.Imaging.Pen.Brush that determines attributes of this Aspose.Imaging.Pen.

public Brush Brush { get; set; }

Property Value

Brush

Exceptions

ArgumentException

The Aspose.Imaging.Pen.Brush property is set on an immutable Aspose.Imaging.Pen, such as those returned by the Aspose.Imaging.Pen class.

Color

Gets or sets the color of this Aspose.Imaging.Pen.

public Color Color { get; set; }

Property Value

Color

Exceptions

ArgumentException

The Aspose.Imaging.Pen.Color property is set on an immutable Aspose.Imaging.Pen, such as those returned by the Aspose.Imaging.Pen class.

CompoundArray

Gets or sets an array of values that specifies a compound pen. A compound pen draws a compound line made up of parallel lines and spaces.

public float[] CompoundArray { get; set; }

Property Value

float[]

Exceptions

ArgumentException

The Aspose.Imaging.Pen.CompoundArray property is set on an immutable Aspose.Imaging.Pen, such as those returned by the Aspose.Imaging.Pen class.

CustomEndCap

Gets or sets a custom cap to use at the end of lines drawn with this Aspose.Imaging.Pen.

public CustomLineCap CustomEndCap { get; set; }

Property Value

CustomLineCap

Exceptions

ArgumentException

The Aspose.Imaging.Pen.CustomEndCap property is set on an immutable Aspose.Imaging.Pen, such as those returned by the Aspose.Imaging.Pen class.

CustomStartCap

Gets or sets a custom cap to use at the beginning of lines drawn with this Aspose.Imaging.Pen.

public CustomLineCap CustomStartCap { get; set; }

Property Value

CustomLineCap

Exceptions

ArgumentException

The Aspose.Imaging.Pen.CustomStartCap property is set on an immutable Aspose.Imaging.Pen, such as those returned by the Aspose.Imaging.Pen class.

DashCap

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

public DashCap DashCap { get; set; }

Property Value

DashCap

Exceptions

ArgumentException

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

DashOffset

Gets or sets the distance from the start of a line to the beginning of a dash pattern.

public float DashOffset { get; set; }

Property Value

float

DashPattern

Gets or sets an array of custom dashes and spaces.

public float[] DashPattern { get; set; }

Property Value

float[]

Exceptions

ArgumentException

The Aspose.Imaging.Pen.DashPattern property is set on an immutable Aspose.Imaging.Pen, such as those returned by the Aspose.Imaging.Pen class.

DashStyle

Gets or sets the style used for dashed lines drawn with this Aspose.Imaging.Pen.

public DashStyle DashStyle { get; set; }

Property Value

DashStyle

Exceptions

ArgumentException

The Aspose.Imaging.Pen.DashStyle property is set on an immutable Aspose.Imaging.Pen, such as those returned by the Aspose.Imaging.Pen class.

EndCap

Gets or sets the cap style used at the end of lines drawn with this Aspose.Imaging.Pen.

public LineCap EndCap { get; set; }

Property Value

LineCap

Exceptions

ArgumentException

The Aspose.Imaging.Pen.EndCap property is set on an immutable Aspose.Imaging.Pen, such as those returned by the Aspose.Imaging.Pen class.

LineJoin

Gets or sets the join style for the ends of two consecutive lines drawn with this Aspose.Imaging.Pen.

public LineJoin LineJoin { get; set; }

Property Value

LineJoin

Exceptions

ArgumentException

The Aspose.Imaging.Pen.LineJoin property is set on an immutable Aspose.Imaging.Pen, such as those returned by the Aspose.Imaging.Pen class.

MiterLimit

Gets or sets the limit of the thickness of the join on a mitered corner.

public float MiterLimit { get; set; }

Property Value

float

Exceptions

ArgumentException

The Aspose.Imaging.Pen.MiterLimit property is set on an immutable Aspose.Imaging.Pen, such as those returned by the Aspose.Imaging.Pen class.

PenType

Gets the style of lines drawn with this Aspose.Imaging.Pen.

[JsonIgnore]
public PenType PenType { get; }

Property Value

PenType

StartCap

Gets or sets the cap style used at the beginning of lines drawn with this Aspose.Imaging.Pen.

public LineCap StartCap { get; set; }

Property Value

LineCap

Exceptions

ArgumentException

The Aspose.Imaging.Pen.StartCap property is set on an immutable Aspose.Imaging.Pen, such as those returned by the Aspose.Imaging.Pen class.

Transform

Gets or sets a copy of the geometric transformation for this Aspose.Imaging.Pen.

public Matrix Transform { get; set; }

Property Value

Matrix

Exceptions

ArgumentException

The Aspose.Imaging.Pen.Transform property is set on an immutable Aspose.Imaging.Pen, such as those returned by the Aspose.Imaging.Pen class.

Width

Gets or sets the width of this Aspose.Imaging.Pen, in units of the Graphics object used for drawing.

public float Width { get; set; }

Property Value

float

Exceptions

ArgumentException

The Aspose.Imaging.Pen.Width property is set on an immutable Aspose.Imaging.Pen, such as those returned by the Aspose.Imaging.Pen class.

Methods

Equals(object)

Check if objects are equal.

public override bool Equals(object obj)

Parameters

obj object

The other object.

Returns

bool

The equality comparison result.

Equals(Pen)

Check if objects are equal.

protected bool Equals(Pen other)

Parameters

other Pen

The other object.

Returns

bool

The equality comparison result.

GetHashCode()

Get hash code of the current object.

public override int GetHashCode()

Returns

int

The hash code.

MultiplyTransform(Matrix)

Multiplies the transformation matrix for this Aspose.Imaging.Pen by the specified Aspose.Imaging.Matrix.

public void MultiplyTransform(Matrix matrix)

Parameters

matrix Matrix

The Aspose.Imaging.Matrix object by which to multiply the transformation matrix.

MultiplyTransform(Matrix, MatrixOrder)

Multiplies the transformation matrix for this Aspose.Imaging.Pen by the specified Aspose.Imaging.Matrix in the specified order.

public void MultiplyTransform(Matrix matrix, MatrixOrder order)

Parameters

matrix Matrix

The Aspose.Imaging.Matrix by which to multiply the transformation matrix.

order MatrixOrder

The order in which to perform the multiplication operation.

ResetTransform()

Resets the geometric transformation matrix for this Aspose.Imaging.Pen to identity.

public void ResetTransform()

RotateTransform(float)

Rotates the local geometric transformation by the specified angle. This method prepends the rotation to the transformation.

public void RotateTransform(float angle)

Parameters

angle float

The angle of rotation.

RotateTransform(float, MatrixOrder)

Rotates the local geometric transformation by the specified angle in the specified order.

public void RotateTransform(float angle, MatrixOrder order)

Parameters

angle float

The angle of rotation.

order MatrixOrder

A Aspose.Imaging.MatrixOrder that specifies whether to append or prepend the rotation matrix.

ScaleTransform(float, float)

Scales the local geometric transformation by the specified factors. This method prepends the scaling matrix to the transformation.

public void ScaleTransform(float sx, float sy)

Parameters

sx float

The factor by which to scale the transformation in the x-axis direction.

sy float

The factor by which to scale the transformation in the y-axis direction.

ScaleTransform(float, float, MatrixOrder)

Scales the local geometric transformation by the specified factors in the specified order.

public void ScaleTransform(float sx, float sy, MatrixOrder order)

Parameters

sx float

The factor by which to scale the transformation in the x-axis direction.

sy float

The factor by which to scale the transformation in the y-axis direction.

order MatrixOrder

A Aspose.Imaging.MatrixOrder that specifies whether to append or prepend the scaling matrix.

SetLineCap(LineCap, LineCap, DashCap)

Sets the values that determine the style of cap used to end lines drawn by this Aspose.Imaging.Pen.

public void SetLineCap(LineCap startCap, LineCap endCap, DashCap dashCap)

Parameters

startCap LineCap

A Aspose.Imaging.LineCap that represents the cap style to use at the beginning of lines drawn with this Aspose.Imaging.Pen.

endCap LineCap

A Aspose.Imaging.LineCap that represents the cap style to use at the end of lines drawn with this Aspose.Imaging.Pen.

dashCap DashCap

A Aspose.Imaging.LineCap that represents the cap style to use at the beginning or end of dashed lines drawn with this Aspose.Imaging.Pen.

TranslateTransform(float, float)

Translates the local geometric transformation by the specified dimensions. This method prepends the translation to the transformation.

public void TranslateTransform(float dx, float dy)

Parameters

dx float

The value of the translation in x.

dy float

The value of the translation in y.

TranslateTransform(float, float, MatrixOrder)

Translates the local geometric transformation by the specified dimensions in the specified order.

public void TranslateTransform(float dx, float dy, MatrixOrder order)

Parameters

dx float

The value of the translation in x.

dy float

The value of the translation in y.

order MatrixOrder

The order (prepend or append) in which to apply the translation.