• Stars
    star
    382
  • Rank 111,677 (Top 3 %)
  • Language
    C#
  • License
    MIT License
  • Created almost 5 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An SVG rendering library.

Svg.Skia

Gitter

Build Status CI

NuGet NuGet MyGet

GitHub release Github All Releases Github Releases

Svg.Skia is an SVG rendering library.

About

Svg.Skia can be used as a .NET library or as a CLI application to render SVG files based on a static SVG Full 1.1 subset to raster images or to a backend's canvas.

The Svg.Skia is using SVG library to load Svg object model.

The Svg.Skia library is implemented using the SkiaSharp rendering backend that aims to be on par or more complete than the original System.Drawing implementation and more performant and cross-platform.

The Svg.Skia can be used in same way as the SkiaSharp.Extended.Svg (load svg files as SKPicture).

The Svg library has a more complete implementation of the Svg document model than SkiaSharp.Extended.Svg and the Svg.Skia renderer will provide more complete rendering subsystem implementation.

NuGet

Svg.Skia is delivered as a NuGet package.

You can find the packages here NuGet and install the package like this:

Install-Package Svg.Skia

or by using nightly build feed:

  • Add https://www.myget.org/F/svgskia-nightly/api/v2 to your package sources
  • Alternative nightly build feed https://pkgs.dev.azure.com/wieslawsoltes/GitHub/_packaging/Nightly/nuget/v3/index.json
  • Update your package using Svg.Skia feed

and install the package like this:

Install-Package Svg.Skia -Pre

Usage

Library

Install Package

dotnet add package Svg.Skia
Install-Package Svg.Skia

Draw on Canvas

using SkiaSharp;
using Svg.Skia;

var svg = new SKSvg();

svg.Load("image.svg");

SKCanvas canvas = ...
canvas.DrawPicture(svg.Picture);

Save as Png

using SkiaSharp;
using Svg.Skia;

using (var svg = new SKSvg())
{
    if (svg.Load("image.svg") is { })
    {
        svg.Save("image.png", SKEncodedImageFormat.Png, 100, 1f, 1f);
    }
}
using System.IO;
using SkiaSharp;
using Svg.Skia;

using (var svg = new SKSvg())
{
    if (svg.Load("image.svg") is { })
    {
        using (var stream = File.OpenWrite("image.png"))
        {
            svg.Picture.ToImage(stream, SKColors.Empty, SKEncodedImageFormat.Png, 100, 1f, 1f);
        }
    }
}
using SkiaSharp;
using Svg.Skia;

using (var svg = new SKSvg())
{
    if (svg.Load("image.svgz") is { })
    {
        svg.Save("image.png", SKEncodedImageFormat.Png, 100, 1f, 1f);
    }
}
using System.IO;
using SkiaSharp;
using Svg.Skia;

using (var svg = new SKSvg())
{
    if (svg.Load("image.svgz") is { })
    {
        using (var stream = File.OpenWrite("image.png"))
        {
            svg.Picture.ToImage(stream, SKColors.Empty, SKEncodedImageFormat.Png, 100, 1f, 1f);
        }
    }
}

Save as Pdf

using SkiaSharp;
using Svg.Skia;

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

Save as Xps

using SkiaSharp;
using Svg.Skia;

using (var svg = new SKSvg())
{
    if (svg.Load("image.svg") is { })
    {
        svg.Picture.ToXps("image.xps", SKColors.Empty, 1f, 1f);
    }
}

AvaloniaUI

Install Package

dotnet add package Avalonia.Svg.Skia
Install-Package Avalonia.Svg.Skia

Svg control

<Svg Path="/Assets/__AJ_Digital_Camera.svg"/>

Image control

<Image Source="{SvgImage /Assets/__AJ_Digital_Camera.svg}"/>

Avalonia Previewer

To make controls work with Avalonia Previewer please add the following lines to BuildAvaloniaApp() method:

GC.KeepAlive(typeof(SvgImageExtension).Assembly);
GC.KeepAlive(typeof(Avalonia.Svg.Skia.Svg).Assembly);

The BuildAvaloniaApp() should look similar to this:

public static AppBuilder BuildAvaloniaApp()
{
    GC.KeepAlive(typeof(SvgImageExtension).Assembly);
    GC.KeepAlive(typeof(Avalonia.Svg.Skia.Svg).Assembly);
    return AppBuilder.Configure<App>()
        .UsePlatformDetect()
        .LogToTrace();
}

This is know issue as previewer not always loads all dependencies, especially custom controls in Avalonia xmlns, other solution would be to add xmlns prefix to control with provided assembly path.

AvaloniaUI SkiaSharp Controls

Install Package

dotnet add package Avalonia.Controls.Skia
Install-Package Avalonia.Controls.Skia

Canvas

Usage:

<SKCanvasControl Name="CanvasControl" />
CanvasControl.Draw += (_, e) =>
{
    e.Canvas.DrawRect(SKRect.Create(0f, 0f, 100f, 100f), new SKPaint { Color = SKColors.Aqua });
};

Command-line tool

dotnet tool install -g Svg.Skia.Converter
Svg.Skia.Converter:
  Converts a svg file to an encoded bitmap image.

Usage:
  Svg.Skia.Converter [options]

Options:
  -f, --inputFiles <inputfiles>              The relative or absolute path to the input files
  -d, --inputDirectory <inputdirectory>      The relative or absolute path to the input directory
  -o, --outputDirectory <outputdirectory>    The relative or absolute path to the output directory
  --outputFiles <outputfiles>                The relative or absolute path to the output files
  -p, --pattern <pattern>                    The search string to match against the names of files in the input directory
  --format <format>                          The output image format
  -q, --quality <quality>                    The output image quality
  -b, --background <background>              The output image background
  -s, --scale <scale>                        The output image horizontal and vertical scaling factor
  --scaleX, -sx <scalex>                     The output image horizontal scaling factor
  --scaleY, -sy <scaley>                     The output image vertical scaling factor
  --systemLanguage <systemlanguage>          The system language name as defined in BCP 47
  --quiet                                    Set verbosity level to quiet
  -c, --load-config <load-config>            The relative or absolute path to the config file
  --save-config <save-config>                The relative or absolute path to the config file
  --version                                  Show version information
  -?, -h, --help                             Show help and usage information

Supported formats: png, jpg, jpeg, webp, pdf, xps

SVG to C# Compiler

About

SVGC compiles SVG drawing markup to C# using SkiaSharp as rendering engine. SVGC can be also used as codegen for upcoming C# 9 Source Generator feature.

Demo

Source Generator Usage

Add NuGet package reference to your csproj.

<PropertyGroup>
  <OutputType>Exe</OutputType>
  <TargetFramework>net6.0</TargetFramework>
  <LangVersion>latest</LangVersion>
</PropertyGroup>
<ItemGroup>
  <PackageReference Include="Svg.SourceGenerator.Skia" Version="0.5.0" />
</ItemGroup>

Include svg assets file in your csproj.

<ItemGroup>
  <AdditionalFiles Include="Assets/Sample.svg" NamespaceName="Assets" ClassName="Sample" />
</ItemGroup>

Use generated SKPicture using static Picture property from Sample class.

using SkiaSharp;
using Assets;

public void Draw(SKCanvas canvas)
{
    canvas.DrawPicture(Sample.Picture);
}

Avalonia Usage

csproj

<ItemGroup>
  <AdditionalFiles Include="Assets/__tiger.svg" NamespaceName="AvaloniaSample" ClassName="Tiger" />
</ItemGroup>
<ItemGroup>
  <PackageReference Include="Svg.SourceGenerator.Skia" Version="0.5.0" />
  <PackageReference Include="Avalonia.Controls.Skia" Version="0.5.0" />
</ItemGroup>

xaml

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:AvaloniaSample;assembly=AvaloniaSample"
        xmlns:skp="clr-namespace:Avalonia.Controls.Skia;assembly=Avalonia.Controls.Skia"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        Width="900" Height="650" WindowStartupLocation="CenterScreen"
        x:Class="AvaloniaSample.MainWindow"
        Title="AvaloniaSample">
    <Window.Resources>
        <skp:SKPictureImage x:Key="TigeImage" Source="{x:Static local:Tiger.Picture}" />
    </Window.Resources>
    <Grid>
        <Image Source="{StaticResource TigeImage}" />
    </Grid>
</Window>

svgc Usage

svgc:
  Converts a svg file to a C# code.

Usage:
  svgc [options]

Options:
  -i, --inputFile <inputfile>      The relative or absolute path to the input file [default: ]
  -o, --outputFile <outputfile>    The relative or absolute path to the output file [default: ]
  -j, --jsonFile <jsonfile>        The relative or absolute path to the json file [default: ]
  -n, --namespace <namespace>      The generated C# namespace name [default: Svg]
  -c, --class <class>              The generated C# class name [default: Generated]
  --version                        Show version information
  -?, -h, --help                   Show help and usage information

Json File Format

[
    { "InputFile":"file1.svg", "OutputFile":"file1.svg.cs", "Class":"ClassName1", "Namespace":"NamespaceName" },
    { "InputFile":"file2.svg", "OutputFile":"file2.svg.cs", "Class":"ClassName2", "Namespace":"NamespaceName" }
]

Links

Build

To build the projects you need to install .NET 5.0 version SDK 5.0.100.

git clone [email protected]:wieslawsoltes/Svg.Skia.git
cd Svg.Skia
git submodule update --init --recursive
dotnet build -c Release

Publish Managed

cd ./src/Svg.Skia.Converter
dotnet publish -c Release -f net6.0 -r win7-x64 /p:PublishTrimmed=True /p:PublishReadyToRun=True -o Svg.Skia.Converter_net6.0_win7-x64
cd ./src/Svg.Skia.Converter
dotnet publish -c Release -f net6.0 -r ubuntu.14.04-x64 /p:PublishTrimmed=True /p:PublishReadyToRun=True -o Svg.Skia.Converter_net6.0_ubuntu.14.04-x64
cd ./src/Svg.Skia.Converter
dotnet publish -c Release -f net6.0 -r osx.10.12-x64 /p:PublishTrimmed=True /p:PublishReadyToRun=True -o Svg.Skia.Converter_net6.0_osx.10.12-x64
cd ./src/Svg.Skia.Converter
dotnet publish -c Release -f net6.0 -r debian.8-x64 /p:PublishTrimmed=True /p:PublishReadyToRun=True -o Svg.Skia.Converter_net6.0_debian.8-x64
cd ./src/SvgToPng
dotnet publish -c Release -f net6.0 -r win7-x64 -o SvgToPng_net6.0_win7-x64
cd ./src/SvgXml.Diagnostics
dotnet publish -c Release -f net6.0 -r win7-x64 -o SvgXml.Diagnostics_net6.0_win7-x64

Publish Native

cd ./src/Svg.Skia.Converter
dotnet publish -c Release -f net6.0 -r win-x64 -o Svg.Skia.Converter_net6.0_win-x64
cd ./src/Svg.Skia.Converter
dotnet publish -c Release -f net6.0 -r linux-x64 -o Svg.Skia.Converter_net6.0_linux-x64
cd ./src/Svg.Skia.Converter
dotnet publish -c Release -f net6.0 -r osx-x64 -o Svg.Skia.Converter_net6.0_osx-x64

Externals

The Svg.Skia library is using code from the https://github.com/vvvv/SVG

License

Parts of Svg.Skia source code are adapted from the https://github.com/vvvv/SVG

Svg.Skia is licensed under the MIT license.

More Repositories

1

ChatGPT

A ChatGPT C# client for MacOS, Windows, Linux, Android, iOS and Browser. Powered by Avalonia UI framework.
C#
1,376
star
2

Core2D

A multi-platform data driven 2D diagram editor.
C#
835
star
3

Dock

A docking layout system.
C#
771
star
4

PanAndZoom

Pan and zoom control for Avalonia.
C#
285
star
5

ColorPicker

Avalonia ColorPicker control
C#
174
star
6

BatchEncoder

BatchEncoder is an audio files conversion software.
C++
145
star
7

NodeEditor

A node editor control for Avalonia.
C#
144
star
8

NXUI

NXUI (nex-ui), next-gen UI - Create minimal Avalonia applications using C# 10 and .NET 6, 7 & 8
C#
136
star
9

ReactiveHistory

Reactive undo/redo framework for .NET.
C#
130
star
10

Avalonia.ThemeManager

Theme manager for Avalonia applications.
C#
87
star
11

AvaloniaDockApplication

Sample Dock application.
C#
60
star
12

wavtoac3encoder

WAV to AC3 Encoder an audio encoder which generates compressed audio streams based on ATSC A/52 specification
C++
35
star
13

HackerNews

HackerNews reader.
C#
34
star
14

DataBox

A DataGrid control based on ListBox control
C#
31
star
15

TreeDataGridEx

TreeDataGridEx is an experimental version of TreeDataGrid for Avalonia with added XAML syntax.
C#
30
star
16

SvgToXaml

Svg to xaml conveter.
C#
23
star
17

Avalonia.Skia.Lottie

An lottie animation player control for Avalonia.
C#
22
star
18

SpiroNet

The .NET C# port of libspiro - conversion between spiro control points and bezier's.
C#
19
star
19

SimpleWavSplitter

Split multi-channel WAV files into single channel WAV files.
C#
18
star
20

PackageReferenceEditor

MSBuild, csproj and props package reference editor.
C#
18
star
21

AwesomeWebAvalonia

Awesome Web Avalonia
17
star
22

EffectsDemo

Effect Demo for Avalonia using SkiaSharp
C#
16
star
23

HexView

HexView control for Avalonia.
C#
13
star
24

Draw2D

C#
11
star
25

WebDemos

C#
11
star
26

WpfUnitTests

Unit tests for WPF controls
C#
9
star
27

Spreadsheet

An experimental virtualized rows and columns DataGrid control created using ItemsRepeater/ListBox.
C#
9
star
28

VariableSizedWrapGrid

VariableSizedWrapGrid is a port of Windows UWP version of VariableSizedWrapGrid control for Avalonia based on WPF implemantation.
C#
8
star
29

MicroStationTagExplorer

MicroStation Tag Explorer
C#
7
star
30

Trace

Trace is an application for transforming bitmaps into vector graphics using BitmapToVector library.
C#
7
star
31

RivePlayer

A rive animation player control for Avalonia.
C#
7
star
32

YawPitchRollTransform

Yaw, Pitch and Roll transform for Avalonia
C#
6
star
33

AvaloniaDialog

C#
6
star
34

FastDraw2D

C#
6
star
35

Avalonia.Xaml.Behaviors

C#
6
star
36

AvaloniaWhatKey

Press any key to see Key enum value for Avalonia UI framework.
C#
5
star
37

DotNetCompress

An .NET compression tool. Supported file formats are Brotli and GZip.
C#
5
star
38

AvaloniaTutorialSamples

C#
5
star
39

wrpc

A Graphical User Interface for using the Wasabi Wallet RPC
C#
5
star
40

CustomControlDemo

Custom control demo for AvaloniaUI
C#
5
star
41

AvsDec

Decode avisynth audio stream to raw audio file and split multi-channel WAV files into single channel WAV files.
C++
5
star
42

MfcToolkit

MFC toolkit.
C++
4
star
43

AftenWindowsBuilds

๏ปฟAften Windows Win32 and Win64 builds.
C
4
star
44

LayoutDemo

Experimental layout controls for AvaloniaUI
C#
4
star
45

LanguageEditor

A dedicated xml language translation files editor.
C#
4
star
46

PaletteGenerator

Palette generator using K-Means clustering algorithm for getting colors from images
C#
4
star
47

CompositorDemos

Avalonia compositor demos
C#
4
star
48

BasicDemo

C#
4
star
49

lols

Performance test: LOLs per second
C#
3
star
50

ImplicitCanvasAnimations

C#
3
star
51

SourceGenerators

Svg to C# Source Generators
C#
3
star
52

TemplatedDataGrid

A DataGrid control based on ListBox and Grid panels.
C#
3
star
53

GesturesDemo

Avalonia gesture recognizers generated by ChatGPT
C#
3
star
54

TypeArgumentsDemo

C#
2
star
55

TilePanelDemo

Experimental tile panel control.
C#
2
star
56

XamlAdorner

Set control adorner from xaml in Avalonia
C#
2
star
57

AttachedPropertyDemo

C#
2
star
58

VectorPaint

C#
2
star
59

SmoothPanelSample

C#
2
star
60

XmlParser

Experimental Xml parser for .NET
C#
2
star
61

CustomVirtualizingLayout

C#
2
star
62

KnobControlDemo

C#
2
star
63

DraggableControls

Draggable controls demo.
C#
2
star
64

SimpleDraw

C#
2
star
65

ReactiveNavigation

An navigation control for Avalonia
C#
2
star
66

GridDemo

Experimental tile control for Avalonia based on Grid panel.
C#
2
star
67

TestCustomDrawing

C#
2
star
68

CatML

A CatML is an alternative Xaml controls toolkit for Avalonia.
C#
2
star
69

SqlExtensions

C#
2
star
70

NonAffineImageTransform2

Port of Non-Affine Transforms in 2D by Charles Petzold to WPF
C#
2
star
71

MorphingDemo

Geometry morphing demo for AvaloniaUI
C#
1
star
72

SvgGradientsDemo

C#
1
star
73

TreeDemo

TreeDataGrid node tree demo
C#
1
star
74

StylingParentRepro

C#
1
star
75

TypefaceUtil

An OpenType typeface utilities.
C#
1
star
76

BatchEncoderWx

BatchEncoder is an audio files conversion software (wxWidgets Demo).
C++
1
star
77

DbDemo

C#
1
star
78

AvaloniaMinimized

C#
1
star
79

DataGridDemo

C#
1
star
80

LineEditor

Line Editor
C#
1
star
81

RenderLoopTaskDemo

C#
1
star
82

HelloAvaloniaNdc

C#
1
star
83

Svg.Maui

Svg rendering support for Microsoft.Maui.Graphics
C#
1
star
84

WalletWasabiSandbox

C#
1
star
85

AvaloniaResponsiveLayout

C#
1
star
86

LogicSimulator

Asynchronous event-driven digital logic simulator
C#
1
star
87

InertiaDemo

C#
1
star
88

SpringAnimator

C#
1
star
89

PerspectiveDemo

C#
1
star
90

TicTacToeAI

C#
1
star
91

Painter

C#
1
star
92

merge

C#
1
star
93

DirectorySize

C#
1
star
94

Pong

Pong console and Avalonia game generated by ChatGPT
C#
1
star
95

FuzzyXaml

C#
1
star
96

PriorityChart

C#
1
star
97

Breakout

Atari Breakout game made by ChatGPT using Avalonia
C#
1
star
98

Spacewar

Spacewar! game made by ChatGPT using Avalonia
C#
1
star
99

ColorBlender

A .NET library for color matching and palette design.
C#
1
star
100

Paint

C#
1
star