Path Class

Represents the path of a custom shape to be drawn or filled. The path can consist of multiple open or closed figures.

See the online help for an example with image.

Definition

Namespace: KGySoft.Drawing.Shapes
Assembly: KGySoft.Drawing.Core (in KGySoft.Drawing.Core.dll) Version: 9.0.0
C#
public sealed class Path
Inheritance
Object    Path

Remarks

Though you can use the dedicated methods of the BitmapDataExtensions class to draw or fill simple shapes, the Path class provides a more effective way to define custom shapes. The Path class can be used to define complex shapes consisting of multiple figures, which can be then drawn or filled in a single operation.

But even if you need to draw or fill a simple shape multiple times, using a Path instance can be more effective, because the region of the path can be cached for faster drawing. The caching can be enabled by setting the PreferCaching property to .

  Note

Please note that in some cases , the Path class can be less effective than using the dedicated methods of the BitmapDataExtensions class. For example, drawing 1 pixel wide lines with no anti-aliasing may use a different algorithm that never uses caching. Also, very large regions may not be cached (this can be adjusted by the DrawingOptions.CacheRegionLimit property).

Examples

The following example demonstrates how to create a Path instance to draw a custom shape:

C#
// It supports flow syntax, so you could even inline it into a Draw/FillPath call:
var path = new Path()
    .TransformTranslation(1, 1)
    .AddPolygon(new(50, 0), new(79, 90), new(2, 35), new(97, 35), new(21, 90))
    .AddEllipse(0, 0, 100, 100)
    .AddRoundedRectangle(0, 0, 100, 100, cornerRadius: 10);

// Calculating the required size of the bitmap, adding symmetric padding:
var bounds = path.Bounds;
var size = bounds.Size + new Size(bounds.Location) * 2;

// Now creating a managed bitmap data but you can also use the GetReadWriteBitmapData
// extensions of the dedicated packages for a GDI+ Bitmap, WPF WriteableBitmap, SKBitmap, etc.
using var bitmapData = BitmapDataFactory.CreateBitmapData(size);
bitmapData.Clear(Color.Cyan);

// Using implicit 1 pixel wide solid pen and default drawing options:
bitmapData.DrawPath(Color.Blue, path);

The example above produces the following result:
Custom path drawn with a 1 pixel wide pen and default options.

Constructors

Path(Boolean) Initializes a new instance of the Path class.
Path(Path) Initializes a new instance of the Path class by copying the content of another instance.

Properties

Bounds Gets the bounds of this Path in pixels required for filling it by a Brush. When drawing, the returned bounds needed to be inflated depending on the corresponding Pen.Width.
IsEmpty Gets whether this Path instance is empty.
PreferCaching Gets or sets whether the region of the path is allowed to be cached for faster drawing.
Default value: , unless it was set to in the constructor.
See also the DrawingOptions.CacheRegionLimit property.
Transformation Gets the currently active transformation matrix that is applied to the items that are added to this Path.

Methods

AddArc(RectangleF, Single, Single) Adds an elliptical arc to this Path.
AddArc(Single, Single, Single, Single, Single, Single) Adds an elliptical arc to this Path.
AddBezier(PointF, PointF, PointF, PointF) Adds a Bézier curve to this Path.
AddBezier(Single, Single, Single, Single, Single, Single, Single, Single) Adds a Bézier curve to this Path.
AddBeziers(IEnumerablePointF) Adds a series of Bézier curves to this Path.
AddBeziers(PointF) Adds a series of Bézier curves to this Path.
AddEllipse(RectangleF) Adds an ellipse to this Path.
AddEllipse(Single, Single, Single, Single) Adds an ellipse to this Path.
AddLine(PointF, PointF) Adds a line to the current figure of this Path instance.
AddLine(Single, Single, Single, Single) Adds a line to the current figure of this Path instance.
AddLines(IEnumerablePointF) Adds a series of connected lines to the current figure of this Path instance.
AddLines(PointF) Adds a series of connected lines to the current figure of this Path instance.
AddPath Adds the figures of another Path instance to this Path.
AddPie(RectangleF, Single, Single) Adds a pie shape to this Path.
AddPie(Single, Single, Single, Single, Single, Single) Adds a pie shape to this Path.
AddPoint(PointF) Adds a point to the current figure of this Path instance.
AddPoint(Single, Single) Adds a point to the current figure of this Path instance.
AddPolygon(IEnumerablePointF) Adds a polygon to this Path.
AddPolygon(PointF) Adds a polygon to this Path.
AddRectangle(RectangleF) Adds a rectangle to this Path.
AddRectangle(Single, Single, Single, Single) Adds a rectangle to this Path.
AddRoundedRectangle(RectangleF, Single) Adds a rounded rectangle to this Path, applying the same corner radius to all corners.
AddRoundedRectangle(RectangleF, Single, Single, Single, Single) Adds a rounded rectangle to this Path, applying a custom corner radius to each corner.
AddRoundedRectangle(Single, Single, Single, Single, Single) Adds a rounded rectangle to this Path, applying the same corner radius to all corners.
AddRoundedRectangle(Single, Single, Single, Single, Single, Single, Single, Single) Adds a rounded rectangle to this Path, applying a custom corner radius to each corner.
AsClosed Gets a Path instance from this Path, in which every figure is closed.
CloseFigure Closes the current figure. If the current figure is empty or already closed, this method has no effect.
GetPoints Gets the points of the figures in this Path instance.
ResetTransformation Resets the current Transformation to the identity matrix.
SetTransformation Overwrites the current Transformation with a TransformationMatrix to be applied to the items that are added to the current Path after calling this method.
StartFigure Starts a new figure without closing the current figure. If the current figure is empty, this method has no effect.
See also the Remarks section of the CloseFigure method for details.
Transform Returns a new Path instance, whose figures are transformed by the specified matrix. This method does not change the original path instance. To transform the original instance, use the TransformAdded method instead.
TransformAdded Transforms the already added items in this Path instance by applying the specified matrix. This method does not change the value of the Transformation property.
TransformRotation Applies a rotation to the current Transformation.
TransformScale Applies a scaling (zoom) to the current Transformation.
TransformTranslation Applies a translation (offset) to the origin of the current Transformation.

See Also