Last active
April 6, 2018 13:45
-
-
Save punker76/49d4d3b2faa68189b017818564b55d2d to your computer and use it in GitHub Desktop.
Show SVG at MahApps IconTemplate with SkiaSharp.Svg
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<configuration> | |
<startup> | |
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> | |
</startup> | |
<runtime> | |
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> | |
<dependentAssembly> | |
<assemblyIdentity name="SkiaSharp" publicKeyToken="0738eb9f132ed756" culture="neutral" /> | |
<bindingRedirect oldVersion="0.0.0.0-1.60.0.0" newVersion="1.60.0.0" /> | |
</dependentAssembly> | |
</assemblyBinding> | |
</runtime> | |
</configuration> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<mahAppsMetroSample:MetroWindow x:Class="MahAppsMetroSample.MainWindow" | |
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | |
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | |
xmlns:Controls="http://metro.mahapps.com/winfx/xaml/controls" | |
xmlns:mahAppsMetroSample="clr-namespace:MahAppsMetroSample" | |
x:Name="TheView" | |
Title="MainWindow" | |
Width="525" | |
Height="350"> | |
<Controls:MetroWindow.IconTemplate> | |
<DataTemplate> | |
<ContentControl Content="{Binding ElementName=TheView, Path=SVGVisual}" /> | |
</DataTemplate> | |
</Controls:MetroWindow.IconTemplate> | |
<Grid x:Name="RootGrid"> | |
</Grid> | |
</mahAppsMetroSample:MetroWindow> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.ObjectModel; | |
using System.Windows; | |
using System.Windows.Controls; | |
using System.Windows.Media; | |
using MahApps.Metro.Controls; | |
namespace MahAppsMetroSample | |
{ | |
public partial class MainWindow : MetroWindow | |
{ | |
public MainWindow() | |
{ | |
this.DataContext = this; | |
InitializeComponent(); | |
this.Loaded += MainWindow_Loaded; | |
} | |
private void MainWindow_Loaded(object sender, RoutedEventArgs e) | |
{ | |
var svg = new SkiaSharp.Extended.Svg.SKSvg(); | |
var picture = svg.Load(@"c:\Users\jkarger\Downloads\SVG_logo.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); | |
}; | |
SVGVisual = visual; | |
} | |
public static readonly DependencyProperty SVGVisualProperty = DependencyProperty.Register( | |
"SVGVisual", typeof(SkiaSharp.Views.WPF.SKElement), typeof(MainWindow), new PropertyMetadata(default(SkiaSharp.Views.WPF.SKElement))); | |
public SkiaSharp.Views.WPF.SKElement SVGVisual | |
{ | |
get { return (SkiaSharp.Views.WPF.SKElement) GetValue(SVGVisualProperty); } | |
set { SetValue(SVGVisualProperty, value); } | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<packages> | |
<package id="SkiaSharp" version="1.60.0" targetFramework="net45" /> | |
<package id="SkiaSharp.Svg" version="1.59.1" targetFramework="net45" /> | |
<package id="SkiaSharp.Views" version="1.60.0" targetFramework="net45" /> | |
</packages> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment