Skip to content

Image annotation

Open a screenshot or photo, mark it up, and export a flattened image or an SVG that keeps the annotations editable. The picture is an element in the document like any other, so annotations sit above it, move with the selection tool and survive a round trip through the file format.

Empty drawing canvas.
The picture sits on its own locked layer, below the annotations, so annotating never moves it by accident and the eraser cannot reach it. Eraser is a pixel mask: it hides the annotation ink under it, within the annotations' layer, and is itself an element that undo takes away. Exported SVG references the picture and keeps every arrow, box and note as vector geometry; the other exports flatten the whole thing at twice the document's resolution.

Placing the picture

The browser decodes the picture first, with LoadImageAsync, before any edit begins, so a file it cannot read is refused with a message rather than placed as an invisible picture. Replacing the picture is two edits - remove the old one, add the new - but to the person using it one action, so it is one undo step. A locked layer keeps tools away, not code: the page adds the picture to it directly.

BinkImageSource ready = await _canvas.LoadImageAsync(BinkImageSource.FromBytes(bytes, mediaType, 0, 0));
BinkImageElement placed = BinkImageElement.AtNaturalSize(ready, Vector2.Zero, maxWidth: _document.Settings.Width - 40);

using (_canvas.History.BeginGroup("Open image"))
{
    _document.RemoveElements(previous);
    _document.AddElement(placed with { Description = "Annotated image: " + name }, _imageLayer);   // the locked layer
}

The decode comes before the group on purpose: a history group left open across a browser round trip would disable undo until it closed. For a picture that needs no group, AddImageAsync does both steps at once, as the whiteboard's Insert image does.

A picture's source is a data:, https:, http: or blob: URI. An embedded one must be a raster picture: SVG is refused unless allowed, because an SVG image is a document that can reference other resources. TryCreate checks a URI from elsewhere without throwing, and a large picture can be kept out of the saved document and referenced by URL instead.

if (!BinkImageSource.TryCreate(uri, width, height, allowSvg: false, out BinkImageSource? source))
{
    // refuse it
}

Layers, hiding and cropping

The document has two layers: Image, locked, and Annotations above it, which is where new marks go (ActiveLayerId). Hide picture hides the image layer to show the annotations alone. Crop edges sets the picture's Crop, a rectangle in source pixels, and shrinks its frame to match, so what stays visible stays where it was under the annotations. Cropping, like moving or resizing, is a vector property: the pixels are never re-encoded, and removing the crop brings the margin back.

_document.UpdateLayer(_imageLayer, p => p with { IsVisible = false });

_document.ReplaceElement(picture with
{
    Crop = new BinkRect(64, 40, 512, 320),   // in source pixels
    Rect = shownPart,
});

PNG, JPEG, WebP or SVG

The browser encodes the raster formats, on an offscreen canvas, so real fonts and every picture format it can decode are included. JPEG and WebP are far smaller than PNG for a drawing over a photo; JPEG has no transparency, so a transparent background exports as white, and Safari has no WebP encoder, so there the export throws NotSupportedException rather than handing back a PNG under a WebP name. SVG keeps every annotation editable, with the descriptions as title elements for screen readers.

var options = new BinkPngExportOptions { Region = BinkExportRegion.Content, Scale = 2f };

await _canvas.SavePngAsync("annotated.png", options);
await _canvas.SaveImageAsync("annotated.jpg", BinkImageFormat.Jpeg, options, quality: 0.85);
byte[] webp = await _canvas.ExportImageAsync(BinkImageFormat.Webp, options, quality: 0.8);
await _canvas.SaveSvgAsync("annotated.svg", new BinkSvgExportOptions { IncludeElementDescriptions = true });

Every export takes a region - Page, Content (cropped to what is drawn, plus Padding), Selection or Custom - and a Background, transparent if wanted. MaxPixels makes an export fail rather than allocate more. The pure .NET BinkPngExporter, which a server uses, also writes PixelsPerMeter into the file (11,811 is about 300 DPI) and takes a Compression level. Scale is independent of the screen: a drawing made on a phone exports at the same resolution as one made on a desktop.

An unhandled error has occurred. Reload 🗙