Class Canvas
Provides a mutable, in-place drawing surface backed by an ImageBuffer. All drawing operations modify the underlying image directly and return the same Canvas instance for fluent method chaining.
public sealed class Canvas
- Inheritance
-
Canvas
- Inherited Members
Examples
using LMKit.Graphics.Drawing;
using LMKit.Graphics.Geometry;
using LMKit.Graphics.Primitives;
using LMKit.Media.Image;
// Create an 800x600 RGBA image and wrap it in a Canvas.
var image = new ImageBuffer(800, 600, ImagePixelFormat.RGBA32);
var canvas = new Canvas(image);
// Fluent chaining with simple overloads.
canvas
.Clear(new Color32(255, 255, 255))
.FillRectangle(Rectangle.FromSize(10, 10, 200, 100), new Color32(0, 120, 215))
.DrawRectangle(Rectangle.FromSize(10, 10, 200, 100), new Color32(0, 0, 0), 2);
// Advanced overload with full pen control.
var pen = new Pen(new Color32(255, 0, 0), 3)
{
LineJoin = LineJoin.Round,
LineCap = LineCap.Round,
Alignment = PenAlignment.Inside
};
canvas.DrawRectangle(Rectangle.FromSize(50, 150, 300, 200), pen);
image.SaveAsPng("output.png");
Remarks
The Canvas class wraps an existing ImageBuffer and delegates rendering to an optimized internal rasterizer. Supported pixel formats are GRAY8, RGB24, and RGBA32.
Two overload families are offered for each shape operation:
- Simple overloads accept a Color32 (and an optional thickness for strokes), suitable for quick prototyping and common use cases.
- Advanced overloads accept a Pen or Brush, providing full control over line joins, line caps, miter limits, and pen alignment.
This class is not thread-safe. Concurrent drawing from multiple threads requires external synchronization.
Constructors
- Canvas(ImageBuffer)
Initializes a new Canvas that draws onto the specified ImageBuffer.
Properties
- Antialiasing
Gets or sets whether drawing operations use antialiased rendering. When true, shape edges are smoothed using coverage-based alpha blending. When false (the default), shapes are rendered with hard, aliased edges.
- Height
Gets the height of the drawing surface in pixels.
- Image
Gets the underlying ImageBuffer that this canvas draws onto.
- Width
Gets the width of the drawing surface in pixels.
Methods
- Clear(Color32)
Fills the entire drawing surface with the specified color, replacing all existing pixel data.
- DrawEllipse(Ellipse, Pen)
Draws the outline of the specified ellipse using the given pen.
- DrawEllipse(Ellipse, Color32, double)
Draws the outline of the specified ellipse with a solid color and thickness.
- DrawFilledEllipse(Ellipse, Brush, Pen)
Fills the interior of the specified ellipse and then draws its outline, using the given brush and pen.
- DrawFilledEllipse(Ellipse, Color32, Color32, double)
Fills the interior of the specified ellipse and then draws its outline, using a solid fill color and a solid stroke color with the given thickness.
- DrawFilledPolygon(Polygon, Brush, Pen)
Fills the interior of the specified polygon and then draws its outline, using the given brush and pen.
- DrawFilledPolygon(Polygon, Color32, Color32, double)
Fills the interior of the specified polygon and then draws its outline, using a solid fill color and a solid stroke color with the given thickness.
- DrawFilledQuadrilateral(Quadrilateral, Brush, Pen)
Fills the interior of the specified quadrilateral and then draws its outline, using the given brush and pen.
- DrawFilledQuadrilateral(Quadrilateral, Color32, Color32, double)
Fills the interior of the specified quadrilateral and then draws its outline, using a solid fill color and a solid stroke color with the given thickness.
- DrawFilledRectangle(Rectangle, Brush, Pen)
Fills the interior of the specified rectangle and then draws its outline, using the given brush and pen.
- DrawFilledRectangle(Rectangle, Color32, Color32, double)
Fills the interior of the specified rectangle and then draws its outline, using a solid fill color and a solid stroke color with the given thickness.
- DrawFilledRoundedRectangle(RoundedRectangle, Brush, Pen)
Fills the interior of the specified rounded rectangle and then draws its outline, using the given brush and pen.
- DrawFilledRoundedRectangle(RoundedRectangle, Color32, Color32, double)
Fills the interior of the specified rounded rectangle and then draws its outline, using a solid fill color and a solid stroke color with the given thickness.
- DrawLine(Point, Point, Pen)
Draws a line segment between two points using the given pen.
- DrawLine(Point, Point, Color32, double)
Draws a line segment between two points with a solid color and thickness. Uses default stroke settings: Miter, Butt.
- DrawLines(IReadOnlyList<Point>, Pen)
Draws connected line segments (an open polyline) using the given pen. Proper joins are rendered at interior vertices and caps at endpoints.
- DrawLines(IReadOnlyList<Point>, Color32, double)
Draws connected line segments (an open polyline) with a solid color and thickness. Proper joins are rendered at interior vertices and caps at endpoints.
- DrawPolygon(Polygon, Pen)
Draws the outline of the specified polygon using the given pen.
- DrawPolygon(Polygon, Color32, double)
Draws the outline of the specified polygon with a solid color and thickness.
- DrawQuadrilateral(Quadrilateral, Pen)
Draws the outline of the specified quadrilateral using the given pen.
- DrawQuadrilateral(Quadrilateral, Color32, double)
Draws the outline of the specified quadrilateral with a solid color and thickness. Uses default stroke settings: Miter, Butt, and Center.
- DrawRectangle(Rectangle, Pen)
Draws the outline of the specified rectangle using the given pen.
- DrawRectangle(Rectangle, Color32, double)
Draws the outline of the specified rectangle with a solid color and thickness. Uses default stroke settings: Miter, Butt, and Center.
- DrawRoundedRectangle(RoundedRectangle, Pen)
Draws the outline of the specified rounded rectangle using the given pen.
- DrawRoundedRectangle(RoundedRectangle, Color32, double)
Draws the outline of the specified rounded rectangle with a solid color and thickness.
- FillEllipse(Ellipse, Brush)
Fills the interior of the specified ellipse using the given brush.
- FillEllipse(Ellipse, Color32)
Fills the interior of the specified ellipse with a solid color.
- FillPolygon(Polygon, Brush)
Fills the interior of the specified polygon using the given brush and the even-odd fill rule.
- FillPolygon(Polygon, Color32)
Fills the interior of the specified polygon with a solid color using the even-odd fill rule.
- FillQuadrilateral(Quadrilateral, Brush)
Fills the interior of the specified quadrilateral using the given brush.
- FillQuadrilateral(Quadrilateral, Color32)
Fills the interior of the specified quadrilateral with a solid color.
- FillRectangle(Rectangle, Brush)
Fills the interior of the specified rectangle using the given brush.
- FillRectangle(Rectangle, Color32)
Fills the interior of the specified rectangle with a solid color.
- FillRoundedRectangle(RoundedRectangle, Brush)
Fills the interior of the specified rounded rectangle using the given brush.
- FillRoundedRectangle(RoundedRectangle, Color32)
Fills the interior of the specified rounded rectangle with a solid color.