WPF use SVG via SVG and SharpVectors

FredGrit發表於2024-11-22
<Window x:Class="WpfApp39.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        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:svg="http://sharpvectors.codeplex.com/svgc/"
        xmlns:local="clr-namespace:WpfApp39"
        WindowState="Maximized"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Image Grid.Column="0">
            <Image.Source>
                <DrawingImage x:Name="di"/>
            </Image.Source>
        </Image>
        <Image Grid.Column="1" x:Name="img"/> 
    </Grid>
</Window>





using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Svg.Transforms;

namespace WpfApp39
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        string svgFile = @"../../1.svg";
        string svgFile2 = @"../../2.svg";
        int width, height;

        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            width = (int)this.ActualWidth / 2;
            height = (int)this.ActualHeight / 2;
            di.Drawing = ConvertSVGToDrawing(svgFile);
            img.Source = ConvertSVGToImageSource(svgFile2);
        }

        private Drawing ConvertSVGToDrawing(string svgFile)
        { 
            SharpVectors.Renderers.Wpf.WpfDrawingDocument doc = new SharpVectors.Renderers.Wpf.WpfDrawingDocument();
            SharpVectors.Renderers.Wpf.WpfDrawingSettings settings = new SharpVectors.Renderers.Wpf.WpfDrawingSettings();
            SharpVectors.Renderers.Wpf.WpfDrawingRenderer renderer = new SharpVectors.Renderers.Wpf.WpfDrawingRenderer(settings);
            SharpVectors.Renderers.Utils.WpfSvgWindow win = new SharpVectors.Renderers.Utils.WpfSvgWindow(1000, 500, renderer);
            renderer.BeginRender(doc);
            win.LoadDocument(svgFile, settings);
            SharpVectors.Dom.Svg.SvgDocument svgDoc = win.Document as SharpVectors.Dom.Svg.SvgDocument;
            renderer.Render(svgDoc);
            Drawing drawing = renderer.Drawing;
            renderer.EndRender();
            return drawing;
        }


        private ImageSource ConvertSVGToImageSource(string filePath, string pngFile = "test.png")
        {
            BitmapImage bmi = new BitmapImage();
            Svg.SvgDocument svgDoc = Svg.SvgDocument.Open(filePath);
            using (var bmp = svgDoc.Draw(width, height))
            {
                bmp.Save(pngFile, ImageFormat.Png);
                bmi.BeginInit();
                bmi.UriSource = new Uri(pngFile, UriKind.RelativeOrAbsolute);
                bmi.EndInit();
                if (bmi.CanFreeze)
                {
                    bmi.Freeze();
                }
            }
            return bmi;
        }
    }
}