原文:将指定路径下的所有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);// 直接追加文件末尾,换行
                    }
                }
            }
        }
    }
}

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

  1. 【Lua】关于遍历指定路径下所有目录及文件

    关于Lua中如何遍历指定文件路径下的所有文件,需要用到Lua的lfs库. 首先创建一个temp.lua文件,用编辑器打开: 要使用lfs库,首先需要把lfs库加载进来 require("lf ...

  2. python如何将指定路径下的某类型文件,返回一个树形结构体,让前端显示为树形的目录结构

    最近遇到一个问题就是某个linux的目录下有各种文件现在的要求是只需要返回.kml格式的文件,并根据前端要求返回如下结构体即:[{'children': [{'children': [{'title' ...

  3. 删除指定路径下固定格式,以.log结尾、三天前的文件,或删除空的日志文件

    师出‘百测’besttest 删除指定路径下固定格式,以.log结尾.三天前的文件,或删除空的日志文件. 日志文件格式:XXXX_2019-01-01.log. import os,datetime ...

  4. C#实现把指定文件夹下的所有文件复制到指定路径下以及修改指定文件的后缀名

    1.实现把指定文件夹下的所有文件复制到指定路径下 public static void copyFiles(string path) { DirectoryInfo dir = new Directo ...

  5. java 压缩文件 传入文件数组,压缩文件,在指定路径下生成指定文件名的压缩文件

    /** * 传入文件数组,压缩文件,在指定路径下生成指定文件名的压缩文件 * * @param files * 文件数组 * @param strZipName * 压缩文件路径及文件名 * @thr ...

  6. python之实现循环查看指定路径下的所有文件---os.walk

    循环查看指定路径下的所有文件.文件夹,包含隐藏文件注:“.filename” 以点开头的是隐藏文件 import os for cur_path,cur_dirs,cur_files in os.wa ...

  7. java监控指定路径下文件及文件夹变化

    之前用jdk7的WatchService API(java.nio.file包)来做目录下的子文件监控,后改为使用commons-io包.主要有下面几点不同:1. WatchService是采用扫描式 ...

  8. Python3在指定路径下递归定位文件中出现的字符串

    [本文出自天外归云的博客园] 脚本功能:在指定的路径下递归搜索,找出指定字符串在文件中出现的位置(行信息). 用到的python特性: 1. PEP 318 -- Decorators for Fun ...

  9. Python获取指定路径下所有文件的绝对路径

    需求 给出制定目录(路径),获取该目录下所有文件的绝对路径: 实现 方式一: import os def get_file_path_by_name(file_dir): ''' 获取指定路径下所有文 ...

随机推荐

  1. Dll的链接使用细节

    关于Dll Dll.Exe 都是PE格式的二进制文件. Dll相当于Linux操作系统下的so文件 1 基地址(Base Address)和相对地址(RelativeVirtual Address) ...

  2. Qt信号槽传递自定义类型参数(qRegisterMetaType)

    1 #include <QMetaType>//记得包含这个头文件 2 //my_type是我自己定义的类型 3 qRegisterMetaType<my_type>(&quo ...

  3. UVALive - 3977 Summits (BFS染色)

    题目大意:坑爹的题目.题意那么难理解. 讲的就是,假设该点是山顶的话(高度为h).那么以该点为中心,往外辐射.走高度大于h-d的点,到达不了还有一个比它高的点 这就提示了,高度要从大到小排序,依次以高 ...

  4. [Recompose] Show a Spinner While a Component is Loading using Recompose

    Learn how to use the 'branch' and 'renderComponent' higher-order components to show a spinner while ...

  5. ios开发网络学习五:MiMEType ,多线程下载文件思路,文件的压缩和解压缩

    一:MiMEType:一般可以再百度上搜索到相应文件的MiMEType,或是利用c语言的api去获取文件的MiMEType : //对该文件发送一个异步请求,拿到文件的MIMEType - (void ...

  6. spring(3)------控制反转(IOC)/依赖注入(DI)

    一.spring核心概念理解 控制反转: 控制反转即IoC (Inversion of Control).它把传统上由程序代码直接操控的对象的调用权交给容器.通过容器来实现对象组件的装配和管理. 所谓 ...

  7. printk()函数的总结

    我们在使用printk()函数中使用日志级别为的是使编程人员在编程过程中自定义地进行信息的输出,更加容易地掌握系统当前的状况.对程序的调试起到了很重要的作用.(下文中的日志级别和控制台日志控制级别是一 ...

  8. 【心情】bjdldrz

    如果我对勇士的记忆停留在73胜10负,

  9. 【t053】整数去位

    Time Limit: 1 second Memory Limit: 128 MB [问题描述] 键盘输入一个高精度的正整数N,去掉其中任意M个数字后剩下的数字按原左右次序将组成一个新的正整数.编程对 ...

  10. 跟我学AngularJs:Service、Factory、Provider依赖注入使用与差别

    林炳文Evankaka原创作品. 转载请注明出处http://blog.csdn.net/evankaka        本教程使用AngularJs版本号:1.5.3        AngularJ ...