Skip to content
demaconsultingPublic

About

Pure DotNet Canvas Library

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

Β 

History

50 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CanvasNet

GitHub forks GitHub stars GitHub contributors License Build Quality Gate Security NuGet

.NET canvas library for loading, saving, and cropping images

Overview

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.

Features

  • πŸ–ΌοΈ Pixel Buffer - Mutable 32-bit RGBA surface with span access
  • βœ‚οΈ Cropping - Independent-copy crop for load/crop/save workflows
  • πŸ“ Resizing - Resize/ResizeToFit with 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; GetInfo reports 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 .pptx slides 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 .vsdx diagrams; unsupported constructs are reported
  • πŸ” Header-Only Probing - GetInfo reads 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

Installation

Install the core package with the .NET CLI (or the equivalent Install-Package command in the Package Manager Console):

dotnet add package DemaConsulting.CanvasNet

Each 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

Usage

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.

Building

pwsh ./build.ps1

API Documentation

Detailed API documentation for all public types and members is distributed in the api/ folder of the NuGet package.

User Guide

The CanvasNet User Guide is available on the CanvasNet releases page.

Contributing

Contributions are welcome. See CONTRIBUTING.md for development setup, coding standards, and the pull request process.

License

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.

Support

About

Pure DotNet Canvas Library

Resources

Code of conduct

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages