将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)

WPF的XAML文档(Main.xaml):

<Window x:Class="SVG2Image.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:local="clr-namespace:SVG2Image"
        mc:Ignorable="d" Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="388,21,0,0" VerticalAlignment="Top"
                Width="103" Click="button_Click" Height="23" />
        <Label x:Name="label" Content="尺寸" HorizontalAlignment="Left" Margin="54,21,0,0" VerticalAlignment="Top" />
        <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="23" Margin="100,21,0,0" TextWrapping="Wrap"
                Text="128,64,32,16" VerticalAlignment="Top" Width="272" />
    </Grid>
</Window>

 

CS代码:(Main.xaml.cs)

using Svg;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
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;

namespace SVG2Image
{
    /// <summary>
    /// 将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        string savePath = @"d:\output";
        string svgFilePath = @"E:\SVG";
        private void button_Click(object sender, RoutedEventArgs e)
        {
            string sizeText = textBox.Text;
            if (string.IsNullOrEmpty(sizeText)) sizeText = "64"; //默认64像素大小
            string[] sizeArray = sizeText.Split(',');
            List<int> listSize = new List<int>();
            try
            {
                foreach (string s in sizeArray)
                {
                    listSize.Add(int.Parse(s));
                }
            }
            catch (Exception exc)
            {
                MessageBox.Show("输入的尺寸有误,必须为数字(多组用逗号隔开);\r\n" + exc.ToString());
                return;
            }

            if (!Directory.Exists(savePath))
            {
                Directory.CreateDirectory(savePath);
            }
            string[] arraySvgFiles = Directory.GetFiles(svgFilePath, "*.svg");
            foreach (string file in arraySvgFiles)
            {
                string fileName = System.IO.Path.GetFileNameWithoutExtension(file);
                try
                {
                    string svgFileContents = File.ReadAllText(file, Encoding.UTF8);
                    var byteArray = Encoding.ASCII.GetBytes(svgFileContents);
                    string saveFileName = savePath + @"\" + fileName + @"{0}_{1}.png";
                    foreach (int sz in listSize)
                    {
                        using (var stream = new MemoryStream(byteArray))
                        {
                            var svgDocument = SvgDocument.Open(stream);
                            svgDocument.Width = sz;
                            svgDocument.Height = sz;
                            var bitmap = svgDocument.Draw();
                            bitmap.Save(string.Format(saveFileName, sz, sz), ImageFormat.Png);
                        }
                    }
                }
                catch (Exception exc)
                {
                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(savePath + @"\error.txt", true))
                    {
                        sw.WriteLine(fileName);// 直接追加文件末尾,换行
                    }
                }
            }
        }
    }
}

发布了399 篇原创文章 · 获赞 223 · 访问量 360万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览