.NET canvas library for loading, saving, and cropping images
CanvasNet is a .NET library providing a mutable, span-based 32-bit RGBA pixel buffer along with
codecs for loading and saving images in common file formats. It's designed for fast, allocation-conscious
image operations using Span<T>, and supports independent-copy cropping and high-quality resizing for
load/crop/resize/save workflows.
- πΌοΈ Pixel Buffer - Mutable 32-bit RGBA surface with span access
- βοΈ Cropping - Independent-copy crop for load/crop/save workflows
- π Resizing -
Resize/ResizeToFitwith Nearest, Bilinear, Bicubic, Lanczos3 and Area filters - π Compositing - Alpha premultiply and Porter-Duff "over" compositing
- π BMP Codec - Load/save 24-bit/32-bit uncompressed BMP files
- π¨ PNG Codec - Load non-interlaced PNGs; save 8-bit RGB/RGBA
- π¨οΈ TIFF Codec - Load/save 8-bit RGB/RGBA/Grayscale TIFF files
- ποΈ JPEG Codec - Load baseline/progressive; save baseline JPEG
- ποΈ GIF Codec - Decode-only load of first GIF frame;
GetInforeports the true frame count - πΌοΈ JPEG 2000 Codec - Decode-only load of JP2 files and raw codestreams
- π SVG Codec - Rasterize a common SVG subset to a surface
- π PDF Document - Open, inspect, and rasterize PDF pages with font substitution
- π½οΈ PowerPoint Rendering - Rasterize
.pptxslides to a surface - π Chart Rendering - Build and paint Bar/Column/Line/Area/Pie/Doughnut charts
- π OOXML Chart Parsing - Parse ECMA-376 DrawingML charts, including from
.pptx - π· Visio Rendering - Rasterize
.vsdxdiagrams; unsupported constructs are reported - π Header-Only Probing -
GetInforeads headers without decoding pixels (GIF excepted) - ποΈ Path Filling - Antialiased nonzero/even-odd fill of vector paths
- ποΈ Stroke-to-Fill - Convert stroked paths into fillable outlines
- π Gradient Paint - Linear or radial gradient fills with spread
- π§± Tile Paint - Fill a path by repeating a pre-rendered tile bitmap at a configurable pitch
- π€ TrueType/CFF Fonts - Load fonts and collections, extract glyph outlines, query metadata
- ποΈ System Font Discovery - Match a family/style against installed fonts, with a bundled fallback
- π¬ Rendering - Transform-aware canvas with text and shape drawing
- β‘ Span-Based - Fast, allocation-conscious pixel and row access
- π Multi-Target - Supports .NET 8, 9, and 10
- π¦ NuGet Ready - Easy integration via NuGet package
Install the core package with the .NET CLI (or the equivalent Install-Package command in the
Package Manager Console):
dotnet add package DemaConsulting.CanvasNetEach optional file-format/rendering feature ships as its own additional package - install only the ones you need, the same way as above:
| Package | Adds |
|---|---|
DemaConsulting.CanvasNet.Svg |
SVG rasterization |
DemaConsulting.CanvasNet.Pdf |
PDF document rendering |
DemaConsulting.CanvasNet.Charts |
Chart building/rendering and OOXML chart parsing |
DemaConsulting.CanvasNet.Pptx |
PowerPoint (.pptx) document rendering |
DemaConsulting.CanvasNet.Vsdx |
Visio (.vsdx) diagram rendering |
Create a surface, crop it, and round-trip it through a codec:
using DemaConsulting.CanvasNet.Canvas;
using DemaConsulting.CanvasNet.Codecs;
// Create a surface, set a pixel, and crop an independent copy
using var surface = new Surface(4, 4);
surface[1, 1] = new Rgba32(255, 0, 0, 255);
using var cropped = surface.Crop(0, 0, 2, 2);
// Save as PNG and load it back
PngCodec.Save(surface, "surface.png");
using var reloaded = PngCodec.Load("surface.png");
// Load, crop, resize to fit a 64x64 box, and save
using var photo = PngCodec.Load("photo.png");
using var region = photo.Crop(10, 10, 200, 100);
using var thumbnail = region.ResizeToFit(64, 64, ResamplingMode.Lanczos3);
PngCodec.Save(thumbnail, "thumbnail.png");Open a PDF and render a page (BMP/TIFF/JPEG/GIF/JPEG 2000/SVG codecs, header-only probing, and feature-detection all follow the same pattern):
using DemaConsulting.CanvasNet.Pdf;
// Render paints the page's real content-stream geometry: path fills/strokes, shadings and tiling
// patterns, placed image XObjects, and text (using the page's embedded font, or an
// automatically substituted system/bundled fallback font when none is embedded). Device,
// CalGray/CalRGB/Lab, ICCBased, Indexed, Separation and DeviceN colors are converted to sRGB.
using var pdfDoc = PdfDocument.Open("document.pdf");
var pageInfo = pdfDoc.GetPageInfo(0);
using var pdfSurface = pdfDoc.Render(0, pageInfo.Width, pageInfo.Height);Render a PowerPoint slide and a Visio page (both report constructs they cannot draw rather than skipping them silently):
using DemaConsulting.CanvasNet.Pptx;
using DemaConsulting.CanvasNet.Vsdx;
// EMF/WMF pictures on a slide are skipped; the callback reports each one
using var deck = PptxDocument.Open("slides.pptx");
var pptxOptions = new PptxRenderOptions
{
OnUnsupportedPictureSkipped = ex => Console.WriteLine($"Skipped picture: {ex.Message}"),
};
using var slide = deck.Render(0, 96f, pptxOptions);
// Each recognized-but-unsupported Visio construct is reported with a stable feature token
using var diagram = VsdxDocument.Open("diagram.vsdx");
var vsdxOptions = new VsdxRenderOptions
{
UnsupportedFeatureHandler = f => Console.WriteLine($"{f.Feature}: {f.Message}"),
};
using var page = diagram.Render(0, 150, vsdxOptions);Fill a vector path onto a surface (stroking, gradients, tile paint, fonts, and charts use the same
Drawing/Geometry API):
using DemaConsulting.CanvasNet.Canvas;
using DemaConsulting.CanvasNet.Drawing;
using DemaConsulting.CanvasNet.Geometry;
using System.Numerics;
using var canvas = new Surface(64, 64);
// Build a triangular path
var triangle = new PathBuilder()
.MoveTo(new Vector2(8, 56))
.LineTo(new Vector2(56, 56))
.LineTo(new Vector2(32, 8))
.Close()
.Build();
// Fill the triangle with an antialiased solid color
PathFiller.Fill(canvas, triangle, new Rgba32(0, 128, 255, 255));See the User Guide for the full API reference and additional worked examples, including stroking, gradients, tile paint, font loading/metadata, chart building, OOXML chart parsing, PDF, PowerPoint and Visio rendering, and SVG and JPEG 2000 decoding.
pwsh ./build.ps1Detailed API documentation for all public types and members is distributed in the api/ folder
of the NuGet package.
The CanvasNet User Guide is available on the CanvasNet releases page.
Contributions are welcome. See CONTRIBUTING.md for development setup, coding standards, and the pull request process.
Copyright (c) DEMA Consulting. Licensed under the MIT License. See LICENSE for details.
By contributing to this project, you agree that your contributions will be licensed under the MIT License.
The DemaConsulting.CanvasNet package bundles the Liberation Sans, Liberation Serif, and
Liberation Mono TrueType fonts (12 files total) as embedded resources, used as a last-resort
fallback font by PdfDocument's automatic font-substitution feature. These fonts are Copyright
(c) 2012 Red Hat, Inc., licensed under the SIL Open Font License, Version 1.1; see
src/DemaConsulting.CanvasNet/Fonts/BundledFonts/OFL.txt and
src/DemaConsulting.CanvasNet/Fonts/BundledFonts/README.md for the full license text and
sourcing/provenance details.
The DemaConsulting.CanvasNet package also bundles the Noto Sans, Noto Sans Math, and Noto Sans
Symbols 2 TrueType fonts (NotoSans-Regular.ttf, NotoSansMath-Regular.ttf,
NotoSansSymbols2-Regular.ttf) as embedded resources, used as the dedicated substitute font for
Symbol/ZapfDingbats text by PdfDocument's automatic font-substitution feature. These fonts
are part of the Noto Project, licensed under the SIL Open Font License, Version 1.1; see
src/DemaConsulting.CanvasNet/Fonts/BundledFonts/NotoFonts-OFL.txt and the "Noto Substitute
Fonts" section of src/DemaConsulting.CanvasNet/Fonts/BundledFonts/README.md for the full license
text and sourcing/provenance details.