All formats

Svg.Skia

LanguageC# (.NET Standard 2.0, .NET 8+)
LicenseMIT
Repositorygithub.com/wieslawsoltes/Svg.Skia
DocumentationREADME
Depends onSkiaSharp, Svg.Custom (fork of the SVG.NET object model)
Read / writeread (renders to PNG, JPEG, WebP, PDF, XPS or any SKCanvas) / no

Svg.Skia loads SVG with the SVG.NET object model and converts it to an SKPicture, a recorded list of Skia drawing commands. You can draw that picture on any SkiaSharp canvas or save it as an image or PDF. It runs everywhere SkiaSharp runs, including Linux servers and containers, and has controls for Avalonia, .NET MAUI and Uno Platform.

It covers SVG 1.1 static rendering and a growing part of SVG 2, and also loads .svgz and Android VectorDrawable XML. JavaScript in SVG is off unless you add the optional Svg.Skia.JavaScript package.

Installation#

Shell
dotnet add package Svg.Skia

On Linux also add a SkiaSharp native asset package, e.g. SkiaSharp.NativeAssets.Linux (or SkiaSharp.NativeAssets.Linux.NoDependencies for Alpine and minimal containers), and install fonts plus fontconfig for text.

Basic usage: convert SVG to PNG#

C#
using System;
using SkiaSharp;
using Svg.Skia;

using var svg = new SKSvg();
if (svg.Load("in.svg") is null)
{
    Console.Error.WriteLine("cannot load in.svg");
    return 1;
}

SKRect bounds = svg.Picture!.CullRect;
Console.WriteLine($"{bounds.Width} x {bounds.Height}");

svg.Save("out.png", SKColors.Transparent);
return 0;

Load returns the SKPicture, or null if the file can’t be parsed.

How to save at 2x scale or as JPEG#

Save(path, background, format, quality, scaleX, scaleY):

C#
svg.Save("out@2x.png", SKColors.Empty, SKEncodedImageFormat.Png, 100, 2f, 2f);
svg.Save("out.jpg", SKColors.White, SKEncodedImageFormat.Jpeg, 90, 1f, 1f);  // JPEG has no alpha

How to render SVG at a given width#

Draw the picture on your own surface with a scale transform:

C#
using System;
using System.IO;
using SkiaSharp;
using Svg.Skia;

using var svg = new SKSvg();
svg.Load("in.svg");
SKRect bounds = svg.Picture!.CullRect;

float scale = 512f / bounds.Width;
var info = new SKImageInfo(512, (int)Math.Ceiling(bounds.Height * scale));
using var surface = SKSurface.Create(info);
SKCanvas canvas = surface.Canvas;
canvas.Clear(SKColors.White);
canvas.Scale(scale);
canvas.DrawPicture(svg.Picture);

using SKImage image = surface.Snapshot();
using SKData data = image.Encode(SKEncodedImageFormat.Png, 100);
using var stream = File.OpenWrite("out-512.png");
data.SaveTo(stream);

How to convert SVG to PDF#

C#
using var svg = new SKSvg();
if (svg.Load("in.svg") is { })
{
    svg.Picture!.ToPdf("out.pdf", SKColors.Empty, 1f, 1f);
}

Shapes and text stay vectors in the PDF. ToXps writes XPS the same way.

How to load SVG from a string#

C#
using var svg = new SKSvg();
svg.FromSvg("<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'><rect width='10' height='10'/></svg>");
Console.WriteLine(svg.Picture!.CullRect);

Tips#