原文:将指定路径下的所有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. Spring Cloud项目

    如何使用windows版Docker并在IntelliJ IDEA使用Docker运行Spring Cloud项目   如何使用windows版Docker并在IntelliJ IDEA使用Docke ...

  2. UI组件之TextView及其子类(三)ToggleButton和Switch

    ToggleButton.Switch.CheckBox和RadioButton都是继承自android.widget.CompoundButton,意思是可选择的,因此它们的使用方法都非常类似. C ...

  3. Node.js日志框架选型比較:Bunyan

    前一篇Node.js日志框架选型比較:Winston Bunyan Bunyan(by Trent Mick)是另外一个值得考虑的日志框架,以稍微不同的方式处理结构化,机器可读性被重点对待. 其结果是 ...

  4. phoenixframe平台连接socketserver,并接收返回值的演示样例

    package org.phoenix.cases.webservice; import java.util.LinkedList; import org.phoenix.action.WebElem ...

  5. 事件处理之二:点击事件监听器的五种写法 分类: H1_ANDROID 2013-09-11 10:32 4262人阅读 评论(1) 收藏

    首选方法二! 方法一:写一个内部类,在类中实现点击事件 1.在父类中调用点击事件 bt_dail.setOnClickListener(new MyButtonListener()); 2.创建内部类 ...

  6. php 获取根目录

    在网站根目录的index.php文件里 define('BASE_PATH',str_replace('\\','/',realpath(dirname(__FILE__).'/'))."/ ...

  7. 【u223】放牙刷

    [题目链接]: [题解] 错排公式 f[n] = (n-1)*(f[n-1]+f[n-2]); 这样理解: 要从n-1和n-2递推到n; 假设第n个位置上的数要放在前n-1个位置中的k位置;则有n-1 ...

  8. form表单上传附件的几种方法

    问题描述:在网页开发过程中,当需要上传附件(图片,音频,视频等)时,常规方法是使用form表单进行提交,这里总结一下form表单提交的几种方法. 参考地址:http://www.cnblogs.com ...

  9. 对Fragment切换的优化

    在项目中需要进行Fragment的切换,一直都是用replace()方法来替换Fragment:然后总感觉切换的时候有些卡顿,原来的代码 /** * 切换页面,这里采用回调 * * @param f ...

  10. boost1.59编译安装(可以完全安装,也可定制安装--buildtype=complete,link=static)

    1.下载: 网址:http://sourceforge.net/projects/boost/files/boost/1.59.0/ 选择:boost_1_59_0.7z或者boost_1_59_0. ...