Skip to content

Instantly share code, notes, and snippets.

@Arlorean
Created December 17, 2016 16:02
Show Gist options
  • Save Arlorean/ef9bb3fdec00e9ff522372ce7133c9f8 to your computer and use it in GitHub Desktop.
Save Arlorean/ef9bb3fdec00e9ff522372ce7133c9f8 to your computer and use it in GitHub Desktop.
View SVG in WPF Window using SkiaSharp
using System.Windows;
using System.Windows.Controls;
namespace WpfApplication2 {
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
var svg = new SkiaSharp.SKSvg();
var picture = svg.Load("Tiger.svg");
var visual = new SkiaSharp.Views.WPF.SKElement();
visual.PaintSurface += (o, a) => {
var fit = a.Info.Rect.AspectFit(svg.CanvasSize.ToSizeI());
a.Surface.Canvas.Scale(fit.Width/svg.CanvasSize.Width);
a.Surface.Canvas.DrawPicture(picture);
};
(this.Content as Grid).Children.Add(visual);
}
}
}
@Arlorean
Copy link
Author

  1. Create a new WPF Project.
  2. Edit MainWindow.xaml.cs and put this code in it (Change namespace to match your namespace).
  3. Google Tiger.svg for an example SVG.
  4. Here is the NuGet packages.cfg:
<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="SkiaSharp" version="1.55.1" targetFramework="net452" />
  <package id="SkiaSharp.Svg" version="1.55.1-beta1" targetFramework="net452" />
  <package id="SkiaSharp.Views" version="1.55.1" targetFramework="net452" />
</packages>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment