All formats

SVG.NET

LanguageC# (.NET Standard 2.0, .NET 8+, .NET Framework)
LicenseMicrosoft Public License (MS-PL)
Repositorygithub.com/svg-net/SVG
Documentationsvg-net.github.io/SVG
Depends onSystem.Drawing.Common (rendering), ExCSS
Read / writeyes / yes (SVG 1.1 object model; renders to System.Drawing.Bitmap)

SVG.NET parses SVG into a tree of typed objects (SvgDocument, SvgCircle, SvgPath, SvgText, …) that you can query, change and write back to XML. You can also build documents from scratch in code. It renders with GDI+ through System.Drawing.

Rendering is its weak spot on modern .NET: System.Drawing.Common is supported only on Windows since .NET 6, so Draw() throws on Linux and macOS. For cross-platform rendering use Svg.Skia, which reuses this object model. Reading, editing and writing SVG work everywhere.

Installation#

Shell
dotnet add package Svg

Basic usage: open an SVG and render it to PNG#

C#
using System;
using System.Drawing;
using System.Drawing.Imaging;
using Svg;

var doc = SvgDocument.Open("in.svg");
Console.WriteLine($"{doc.Width} x {doc.Height}");

using Bitmap bitmap = doc.Draw();           // intrinsic size
bitmap.Save("out.png", ImageFormat.Png);

using Bitmap big = doc.Draw(1024, 0);       // 1024 px wide, height keeps the aspect ratio
big.Save("out-1024.png", ImageFormat.Png);

Draw(width, height): if one value is 0 it’s computed from the aspect ratio; if both are given the image is stretched.

How to create an SVG file from code#

C#
using System.Drawing;
using Svg;

var doc = new SvgDocument
{
    Width = 200,
    Height = 100,
    ViewBox = new SvgViewBox(0, 0, 200, 100),
};
doc.Children.Add(new SvgRectangle
{
    X = 10, Y = 10, Width = 80, Height = 80,
    CornerRadiusX = 8, CornerRadiusY = 8,
    Fill = new SvgColourServer(Color.SteelBlue),
});
doc.Children.Add(new SvgCircle
{
    CenterX = 150, CenterY = 50, Radius = 40,
    Fill = new SvgColourServer(Color.Orange),
    Stroke = new SvgColourServer(Color.Black),
    StrokeWidth = 2,
});
doc.Children.Add(new SvgText("Hello") { X = { 20 }, Y = { 95 }, FontSize = 12 });

doc.Write("created.svg");

How to change elements in an existing SVG#

C#
using System.Drawing;
using System.Linq;
using Svg;

var doc = SvgDocument.Open("in.svg");

// by id
var logo = doc.GetElementById<SvgVisualElement>("logo");
if (logo != null) logo.Fill = new SvgColourServer(Color.Green);

// all elements of a type
foreach (var circle in doc.Descendants().OfType<SvgCircle>())
{
    circle.Stroke = new SvgColourServer(Color.Black);
    circle.StrokeWidth = 2;
}

// custom or unknown attributes
if (logo != null) logo.CustomAttributes["data-version"] = "2";

doc.Write("modified.svg");

How to parse SVG from a string#

C#
string xml = "<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'><rect id='r' width='10' height='10'/></svg>";
var doc = SvgDocument.FromSvg<SvgDocument>(xml);
var rect = doc.GetElementById<SvgRectangle>("r");
Console.WriteLine(rect.Width);

How to write SVG to a stream#

C#
using var stream = new MemoryStream();
doc.Write(stream);
string xml = System.Text.Encoding.UTF8.GetString(stream.ToArray());

Tips#