Class DrawingCanvas<TPixel>
- Namespace
- SixLabors.ImageSharp.Drawing.Processing
- Assembly
- SixLabors.ImageSharp.Drawing.dll
A drawing canvas over a frame target.
public sealed class DrawingCanvas<TPixel> : DrawingCanvas, IDisposable where TPixel : unmanaged, IPixel<TPixel>
Type Parameters
TPixelThe pixel format.
- Inheritance
-
DrawingCanvas<TPixel>
- Implements
- Inherited Members
Constructors
DrawingCanvas(Configuration, DrawingOptions, ICanvasFrame<TPixel>, params IPath[])
Initializes a new instance of the DrawingCanvas<TPixel> class.
public DrawingCanvas(Configuration configuration, DrawingOptions options, ICanvasFrame<TPixel> targetFrame, params IPath[] clipPaths)
Parameters
configurationConfigurationThe active processing configuration.
optionsDrawingOptionsInitial drawing options for this canvas instance.
targetFrameICanvasFrame<TPixel>The destination frame.
clipPathsIPath[]Initial clip paths for this canvas instance.
DrawingCanvas(Configuration, DrawingOptions, IDrawingBackend, ICanvasFrame<TPixel>, params IPath[])
Initializes a new instance of the DrawingCanvas<TPixel> class with an explicit backend and initial state.
public DrawingCanvas(Configuration configuration, DrawingOptions options, IDrawingBackend backend, ICanvasFrame<TPixel> targetFrame, params IPath[] clipPaths)
Parameters
configurationConfigurationThe active processing configuration.
optionsDrawingOptionsInitial drawing options for this canvas instance.
backendIDrawingBackendThe drawing backend implementation.
targetFrameICanvasFrame<TPixel>The destination frame.
clipPathsIPath[]Initial clip paths for this canvas instance.
DrawingCanvas(Configuration, DrawingOptions, DrawingTextCache, ICanvasFrame<TPixel>, params IPath[])
Initializes a new instance of the DrawingCanvas<TPixel> class.
public DrawingCanvas(Configuration configuration, DrawingOptions options, DrawingTextCache textCache, ICanvasFrame<TPixel> targetFrame, params IPath[] clipPaths)
Parameters
configurationConfigurationThe active processing configuration.
optionsDrawingOptionsInitial drawing options for this canvas instance.
textCacheDrawingTextCacheThe text drawing cache used by this canvas instance.
targetFrameICanvasFrame<TPixel>The destination frame.
clipPathsIPath[]Initial clip paths for this canvas instance.
DrawingCanvas(Configuration, DrawingOptions, DrawingTextCache, IDrawingBackend, ICanvasFrame<TPixel>, params IPath[])
Initializes a new instance of the DrawingCanvas<TPixel> class with an explicit backend and initial state.
public DrawingCanvas(Configuration configuration, DrawingOptions options, DrawingTextCache textCache, IDrawingBackend backend, ICanvasFrame<TPixel> targetFrame, params IPath[] clipPaths)
Parameters
configurationConfigurationThe active processing configuration.
optionsDrawingOptionsInitial drawing options for this canvas instance.
textCacheDrawingTextCacheThe text drawing cache used by this canvas instance.
backendIDrawingBackendThe drawing backend implementation.
targetFrameICanvasFrame<TPixel>The destination frame.
clipPathsIPath[]Initial clip paths for this canvas instance.
DrawingCanvas(Configuration, DrawingOptions, DrawingTextCache, Buffer2DRegion<TPixel>, params IPath[])
Initializes a new instance of the DrawingCanvas<TPixel> class.
public DrawingCanvas(Configuration configuration, DrawingOptions options, DrawingTextCache textCache, Buffer2DRegion<TPixel> targetRegion, params IPath[] clipPaths)
Parameters
configurationConfigurationThe active processing configuration.
optionsDrawingOptionsInitial drawing options for this canvas instance.
textCacheDrawingTextCacheThe text drawing cache used by this canvas instance.
targetRegionBuffer2DRegion<TPixel>The destination target region.
clipPathsIPath[]Initial clip paths for this canvas instance.
DrawingCanvas(Configuration, DrawingOptions, Buffer2DRegion<TPixel>, params IPath[])
Initializes a new instance of the DrawingCanvas<TPixel> class.
public DrawingCanvas(Configuration configuration, DrawingOptions options, Buffer2DRegion<TPixel> targetRegion, params IPath[] clipPaths)
Parameters
configurationConfigurationThe active processing configuration.
optionsDrawingOptionsInitial drawing options for this canvas instance.
targetRegionBuffer2DRegion<TPixel>The destination target region.
clipPathsIPath[]Initial clip paths for this canvas instance.
Properties
Bounds
Gets the local bounds of this canvas.
public override Rectangle Bounds { get; }
Property Value
SaveCount
Gets the number of saved states currently on the canvas stack.
public override int SaveCount { get; }
Property Value
Methods
Apply(IPath, Action<IImageProcessingContext>)
Applies an image-processing operation to a path region.
public override void Apply(IPath path, Action<IImageProcessingContext> operation)
Parameters
pathIPathThe path region to process.
operationAction<IImageProcessingContext>The image-processing operation to apply to the region.
Remarks
The operation affects only pixels covered by the supplied path.
Apply(IPath, Action<IImageProcessingContext>, GraphicsOptions, Point)
Applies an image-processing operation to a path region and composites the processed pixels back with explicit options, optionally offset from where they were read.
public override void Apply(IPath path, Action<IImageProcessingContext> operation, GraphicsOptions writeBackOptions, Point writeBackOffset)
Parameters
pathIPathThe path region to process.
operationAction<IImageProcessingContext>The image-processing operation to apply to the region.
writeBackOptionsGraphicsOptionsThe graphics options used to composite the processed pixels back.
writeBackOffsetPointThe offset at which the processed pixels are written back.
Remarks
The write-back affects only pixels covered by the supplied path translated by the offset.
Apply(PathBuilder, Action<IImageProcessingContext>)
Applies an image-processing operation to a region described by a path builder.
public override void Apply(PathBuilder pathBuilder, Action<IImageProcessingContext> operation)
Parameters
pathBuilderPathBuilderThe path builder describing the region to process.
operationAction<IImageProcessingContext>The image-processing operation to apply to the region.
Apply(Rectangle, Action<IImageProcessingContext>)
Applies an image-processing operation to a local region.
public override void Apply(Rectangle region, Action<IImageProcessingContext> operation)
Parameters
regionRectangleThe local region to process.
operationAction<IImageProcessingContext>The image-processing operation to apply to the region.
Apply(Rectangle, Action<IImageProcessingContext>, GraphicsOptions, Point)
Applies an image-processing operation to a local region and composites the processed pixels back with explicit options, optionally offset from where they were read. The target region is untouched while the operation runs, so the processed pixels can be blended against the original content; for example a drop shadow composites the tinted, blurred region back beneath the content with DestOver at the shadow offset.
public override void Apply(Rectangle region, Action<IImageProcessingContext> operation, GraphicsOptions writeBackOptions, Point writeBackOffset)
Parameters
regionRectangleThe local region to process.
operationAction<IImageProcessingContext>The image-processing operation to apply to the region.
writeBackOptionsGraphicsOptionsThe graphics options used to composite the processed pixels back.
writeBackOffsetPointThe offset at which the processed pixels are written back.
Clear(Brush, IPath)
Fills a path region with the given brush using replacement composition: the brush output overwrites the covered pixels outright, including their alpha, rather than blending over them.
public override void Clear(Brush brush, IPath path)
Parameters
brushBrushBrush used to shade destination pixels during the clear.
pathIPathThe path region to clear.
Clip(ClipOperation, params IPath[])
Narrows the current clip region by applying the specified clipping operation with the supplied clip paths.
public override void Clip(ClipOperation operation, params IPath[] clipPaths)
Parameters
operationClipOperationThe operation to apply to the current clip.
clipPathsIPath[]The clip paths to combine with the current clip, in local coordinates.
Remarks
The clip paths are transformed by the active transform at the point this is called.
Clip(params IPath[])
Narrows the current clip region by intersecting it with the supplied clip paths.
public override void Clip(params IPath[] clipPaths)
Parameters
clipPathsIPath[]The clip paths to intersect with the current clip, in local coordinates.
Remarks
The clip paths are transformed by the active transform at the point this is called, then intersected with the existing clip; clipping only ever narrows. The resulting clip is part of the current saved state and is restored by Restore(). Multiple paths combine as a union before intersecting (e.g. a region built from several rectangles).
CopyPixelsFrom(DrawingCanvas, Rectangle, Point)
Copies pixels from another canvas into this canvas.
public override void CopyPixelsFrom(DrawingCanvas source, Rectangle sourceRectangle, Point targetPoint)
Parameters
sourceDrawingCanvasThe source canvas.
sourceRectangleRectangleThe source rectangle in source-local coordinates.
targetPointPointThe target point in this canvas' local coordinates.
Remarks
Any queued commands on both canvases are applied before the copy.
CreateRegion(Rectangle)
Creates a child canvas over a subregion in local coordinates.
public override DrawingCanvas<TPixel> CreateRegion(Rectangle region)
Parameters
regionRectangleThe child region in local coordinates.
Returns
- DrawingCanvas<TPixel>
A child canvas with local origin at (0,0).
CreateScene()
Seals the commands recorded so far into a retained backend scene and clears them from the canvas queue.
public override DrawingBackendScene CreateScene()
Returns
- DrawingBackendScene
The created backend scene.
Remarks
The active clip range is closed before the scene is built and reopened afterwards, so this acts as an ordering boundary in the recorded timeline. Image resources retained for the sealed commands are transferred to the returned scene, which owns and disposes them.
Dispose()
Releases the canvas. Disposing the root canvas closes any open layers and clips and replays the recorded command timeline into the target frame; child region canvases share the root's command stream and do not flush on disposal.
public override void Dispose()
Remarks
The root canvas also releases image resources retained for deferred draws and, when it owns the text drawing cache, clears that cache. Disposal is idempotent.
Draw(Pen, IPath)
Draws a path outline in local coordinates using the given pen.
public override void Draw(Pen pen, IPath path)
Parameters
DrawGlyphs(Brush, Pen, IEnumerable<GlyphPathCollection>)
Draws layered glyph geometry.
public override void DrawGlyphs(Brush brush, Pen pen, IEnumerable<GlyphPathCollection> glyphs)
Parameters
brushBrushBrush used to fill glyph layers.
penPenPen used to outline dominant painted layers.
glyphsIEnumerable<GlyphPathCollection>Layered glyph geometry to draw.
DrawImage(Image, Rectangle, RectangleF, WrapMode, WrapMode, IResampler?)
Draws an image source region into a destination rectangle, tiling the painted area by repeating the destination rectangle outwards per the supplied WrapModes.
public override void DrawImage(Image image, Rectangle sourceRect, RectangleF destinationRect, WrapMode wrapX, WrapMode wrapY, IResampler? sampler)
Parameters
imageImageThe source image.
sourceRectRectangleThe source rectangle within
image.destinationRectRectangleFThe destination rectangle in local canvas coordinates (defines a single tile cell).
wrapXWrapModeThe horizontal wrap mode applied when sampling beyond
destinationRect.wrapYWrapModeThe vertical wrap mode applied when sampling beyond
destinationRect.samplerIResamplerOptional resampler used when scaling or transforming the image. Defaults to Bicubic.
DrawImage(Image, Rectangle, RectangleF, IResampler?)
Draws an image source region into a destination rectangle.
public override void DrawImage(Image image, Rectangle sourceRect, RectangleF destinationRect, IResampler? sampler)
Parameters
imageImageThe source image.
sourceRectRectangleThe source rectangle within
image.destinationRectRectangleFThe destination rectangle in local canvas coordinates.
samplerIResamplerOptional resampler used when scaling or transforming the image. Defaults to Bicubic.
DrawImage(Image<TPixel>, Rectangle, RectangleF, WrapMode, WrapMode, IResampler?)
Draws an image source region into a destination rectangle, tiling the painted area by repeating the destination rectangle outwards per the supplied WrapModes.
public void DrawImage(Image<TPixel> image, Rectangle sourceRect, RectangleF destinationRect, WrapMode wrapX, WrapMode wrapY, IResampler? sampler = null)
Parameters
imageImage<TPixel>The source image.
sourceRectRectangleThe source rectangle within
image.destinationRectRectangleFThe destination rectangle in local canvas coordinates (defines a single tile cell).
wrapXWrapModeThe horizontal wrap mode applied when sampling beyond
destinationRect.wrapYWrapModeThe vertical wrap mode applied when sampling beyond
destinationRect.samplerIResamplerOptional resampler used when scaling or transforming the image. Defaults to Bicubic.
DrawImage(Image<TPixel>, Rectangle, RectangleF, IResampler?)
Draws an image source region into a destination rectangle.
public void DrawImage(Image<TPixel> image, Rectangle sourceRect, RectangleF destinationRect, IResampler? sampler = null)
Parameters
imageImage<TPixel>The source image.
sourceRectRectangleThe source rectangle within
image.destinationRectRectangleFThe destination rectangle in local canvas coordinates.
samplerIResamplerOptional resampler used when scaling or transforming the image. Defaults to Bicubic.
DrawLine(Pen, PointF, PointF)
Draws a two-point line segment using the provided pen and drawing options.
public void DrawLine(Pen pen, PointF start, PointF end)
Parameters
penPenPen used to generate the line outline.
startPointFLine start point.
endPointFLine end point.
DrawLine(Pen, params PointF[])
Draws a polyline outline using the provided pen and drawing options.
public override void DrawLine(Pen pen, params PointF[] points)
Parameters
DrawText(LineLayout, IPath, Brush?, Pen?)
Draws one prepared line layout along a path baseline onto this canvas.
public override void DrawText(LineLayout lineLayout, IPath path, Brush? brush, Pen? pen)
Parameters
lineLayoutLineLayoutThe prepared line layout to draw.
pathIPathThe path used as the text baseline in local canvas coordinates.
brushBrushOptional brush used to fill glyphs.
penPenOptional pen used to outline glyphs.
DrawText(LineLayout, PointF, Brush?, Pen?)
Draws one prepared line layout onto this canvas.
public override void DrawText(LineLayout lineLayout, PointF location, Brush? brush, Pen? pen)
Parameters
lineLayoutLineLayoutThe prepared line layout to draw.
locationPointFThe drawing location in local canvas coordinates.
brushBrushOptional brush used to fill glyphs.
penPenOptional pen used to outline glyphs.
DrawText(TextBlock, IPath, float, Brush?, Pen?)
Draws a prepared text block along a path baseline onto this canvas.
public override void DrawText(TextBlock textBlock, IPath path, float wrappingLength, Brush? brush, Pen? pen)
Parameters
textBlockTextBlockThe prepared text block to draw.
pathIPathThe path used as the text baseline in local canvas coordinates.
wrappingLengthfloatThe wrapping length in pixels. Use
-1to disable wrapping.brushBrushOptional brush used to fill glyphs.
penPenOptional pen used to outline glyphs.
DrawText(TextBlock, PointF, float, Brush?, Pen?)
Draws a prepared text block onto this canvas.
public override void DrawText(TextBlock textBlock, PointF location, float wrappingLength, Brush? brush, Pen? pen)
Parameters
textBlockTextBlockThe prepared text block to draw.
locationPointFThe drawing location in local canvas coordinates.
wrappingLengthfloatThe wrapping length in pixels. Use
-1to disable wrapping.brushBrushOptional brush used to fill glyphs.
penPenOptional pen used to outline glyphs.
DrawText(RichTextOptions, ReadOnlySpan<char>, IPath, Brush?, Pen?)
Draws text along a path baseline onto this canvas.
public override void DrawText(RichTextOptions textOptions, ReadOnlySpan<char> text, IPath path, Brush? brush, Pen? pen)
Parameters
textOptionsRichTextOptionsThe text rendering options.
textReadOnlySpan<char>The text to draw.
pathIPathThe path used as the text baseline in local canvas coordinates.
brushBrushOptional brush used to fill glyphs.
penPenOptional pen used to outline glyphs.
DrawText(RichTextOptions, ReadOnlySpan<char>, Brush?, Pen?)
Draws text onto this canvas.
public override void DrawText(RichTextOptions textOptions, ReadOnlySpan<char> text, Brush? brush, Pen? pen)
Parameters
textOptionsRichTextOptionsThe text rendering options.
textReadOnlySpan<char>The text to draw.
brushBrushOptional brush used to fill glyphs.
penPenOptional pen used to outline glyphs.
DrawText(ReadOnlySpan<ushort>, ReadOnlySpan<Vector2>, RichGlyphOptions, Brush?, Pen?)
Draws positioned glyphs onto this canvas.
public override void DrawText(ReadOnlySpan<ushort> glyphIds, ReadOnlySpan<Vector2> points, RichGlyphOptions options, Brush? brush, Pen? pen)
Parameters
glyphIdsReadOnlySpan<ushort>The glyph identifiers.
pointsReadOnlySpan<Vector2>The absolute glyph origins in pixel units.
optionsRichGlyphOptionsThe glyph rendering options, including the font and optional glyph paint.
brushBrushDefault brush used to fill glyphs when Brush is not set.
penPenDefault pen used to outline glyphs when Pen is not set.
DrawText(ushort, RichGlyphOptions, Brush?, Pen?)
Draws a single glyph, identified by its glyph id, onto this canvas.
public override void DrawText(ushort glyphId, RichGlyphOptions options, Brush? brush, Pen? pen)
Parameters
glyphIdushortThe id of the glyph within the font face referenced by
options.optionsRichGlyphOptionsThe glyph rendering options, including the font, origin, grapheme index and optional per-glyph paint.
brushBrushDefault brush used to fill the glyph when Brush is not set.
penPenDefault pen used to outline the glyph when Pen is not set.
Fill(Brush, IPath)
Fills a path in local coordinates using the given brush.
public override void Fill(Brush brush, IPath path)
Parameters
Flush()
Seals the drawing commands recorded so far into an ordered range so that subsequent commands begin a new range.
public override void Flush()
Remarks
This does not rasterize or composite anything into the target frame. Recorded commands are replayed into the target only when the root canvas is disposed, or when they are lowered explicitly through CreateScene(), RenderScene(DrawingBackendScene) or CopyPixelsFrom(DrawingCanvas, Rectangle, Point). Use this to establish an ordering boundary in the recorded timeline without ending the canvas.
MeasureText(RichTextOptions, ReadOnlySpan<char>)
Measures the full set of layout metrics for the supplied text.
public override TextMetrics MeasureText(RichTextOptions textOptions, ReadOnlySpan<char> text)
Parameters
textOptionsRichTextOptionsThe text shaping and layout options.
textReadOnlySpan<char>The text to measure.
Returns
- TextMetrics
A TextMetrics value containing the metrics for the laid-out text.
RenderScene(DrawingBackendScene)
Records a previously created backend scene into this canvas' command timeline.
public override void RenderScene(DrawingBackendScene scene)
Parameters
sceneDrawingBackendSceneThe backend scene to render.
Remarks
The active command range is sealed before the scene is inserted so the scene renders in submission order relative to the commands recorded around it. Like other recorded work, the scene is replayed into the target frame when the root canvas is disposed or otherwise flushed, not at the point of this call.
Restore()
Restores the most recently saved state.
public override void Restore()
Remarks
If the most recently saved state was created by a SaveLayer overload,
the layer is closed and becomes part of subsequent rendering.
RestoreTo(int)
Restores to a specific save count.
public override void RestoreTo(int saveCount)
Parameters
saveCountintThe save count to restore to.
Remarks
State frames above saveCount are discarded,
and the last discarded frame becomes the current state.
If any discarded state was created by a SaveLayer overload,
those layers are closed and become part of subsequent rendering.
Save()
Saves the current drawing state on the state stack.
public override int Save()
Returns
- int
The save count after the state has been pushed.
Remarks
This operation stores the current canvas state by reference. If the same DrawingOptions instance is mutated after Save(), those mutations are visible when restoring.
Save(DrawingOptions)
Saves the current drawing state and replaces the active state with the provided options.
public override int Save(DrawingOptions options)
Parameters
optionsDrawingOptionsDrawing options for the new active state.
Returns
- int
The save count after the previous state has been pushed.
Remarks
The provided options instance is stored by reference.
Mutating it after this call mutates the active/restored state behavior.
SaveLayer(GraphicsOptions, IPath, LayerEffect)
Saves the current drawing state and begins an isolated compositing layer whose content is transformed by an effect confined to a path region when the layer is restored.
public override int SaveLayer(GraphicsOptions layerOptions, IPath region, LayerEffect effect)
Parameters
layerOptionsGraphicsOptionsGraphics options controlling how the closed layer is composited against the parent canvas.
regionIPathThe path region the effect processes, in local coordinates.
effectLayerEffectThe effect applied to the layer content on restore.
Returns
- int
The save count after the layer state has been pushed.
Remarks
The effect processes only pixels covered by the supplied path; its output lands on the path translated by the effect's offset. The layer bounds are derived from the path and expanded by the effect's reach.
SaveLayer(GraphicsOptions, IPath, LayerEffect, DrawingOptions)
Saves the current drawing state and begins an isolated compositing layer whose content is transformed by an effect confined to a path region when the layer is restored, using the supplied drawing options for commands recorded into the layer.
public override int SaveLayer(GraphicsOptions layerOptions, IPath region, LayerEffect effect, DrawingOptions options)
Parameters
layerOptionsGraphicsOptionsGraphics options controlling how the closed layer is composited against the parent canvas.
regionIPathThe path region the effect processes, in local coordinates.
effectLayerEffectThe effect applied to the layer content on restore.
optionsDrawingOptionsDrawing options for the layer contents.
Returns
- int
The save count after the layer state has been pushed.
SaveLayer(GraphicsOptions, Rectangle)
Saves the current drawing state and begins an isolated compositing layer bounded to a subregion. Subsequent draw commands target that layer until Restore() closes it.
public override int SaveLayer(GraphicsOptions layerOptions, Rectangle bounds)
Parameters
layerOptionsGraphicsOptionsGraphics options controlling how the closed layer is composited against the parent canvas.
boundsRectangleThe local bounds of the layer. Only this region is allocated and composited.
Returns
- int
The save count after the layer state has been pushed.
Remarks
The layer bounds are expressed in the current local coordinate system and are transformed with the active drawing transform when the layer is created. They limit allocation and compositing only; they do not change the canvas coordinate system used by commands recorded inside the layer.
SaveLayer(GraphicsOptions, Rectangle, DrawingOptions)
Saves the current drawing state and begins an isolated compositing layer using the supplied drawing options for commands recorded into the layer.
public override int SaveLayer(GraphicsOptions layerOptions, Rectangle bounds, DrawingOptions options)
Parameters
layerOptionsGraphicsOptionsGraphics options controlling how the closed layer is composited against the parent canvas.
boundsRectangleThe local bounds of the layer. Only this region is allocated and composited.
optionsDrawingOptionsDrawing options for the layer contents.
Returns
- int
The save count after the layer state has been pushed.
SaveLayer(GraphicsOptions, Rectangle, LayerEffect)
Saves the current drawing state and begins an isolated compositing layer whose content is transformed by an effect when the layer is restored.
public override int SaveLayer(GraphicsOptions layerOptions, Rectangle bounds, LayerEffect effect)
Parameters
layerOptionsGraphicsOptionsGraphics options controlling how the closed layer is composited against the parent canvas.
boundsRectangleThe content bounds in local canvas coordinates.
effectLayerEffectThe effect applied to the layer content on restore.
Returns
- int
The save count after the layer state has been pushed.
Remarks
The layer isolates the content, so the effect operates on exactly what is drawn between this call and the matching Restore(), against transparency; a DropShadowLayerEffect, for example, slots its shadow beneath that content before the layer composites onto the canvas. The bounds are expanded internally by the effect's reach so blurred or offset output is not cut off.
SaveLayer(GraphicsOptions, Rectangle, LayerEffect, DrawingOptions)
Saves the current drawing state and begins an isolated compositing layer whose content is transformed by an effect when the layer is restored, using the supplied drawing options for commands recorded into the layer.
public override int SaveLayer(GraphicsOptions layerOptions, Rectangle bounds, LayerEffect effect, DrawingOptions options)
Parameters
layerOptionsGraphicsOptionsGraphics options controlling how the closed layer is composited against the parent canvas.
boundsRectangleThe content bounds in local canvas coordinates.
effectLayerEffectThe effect applied to the layer content on restore.
optionsDrawingOptionsDrawing options for the layer contents.
Returns
- int
The save count after the layer state has been pushed.