当前位置:网站首页>Export all SVG files in the specified path into pictures in PNG format (thumbnail or original size)
Export all SVG files in the specified path into pictures in PNG format (thumbnail or original size)
2022-04-23 07:54:00 【Dake mountain man】
WPF Of XAML file (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=" Size " 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 Code :(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>
/// Will specify all the SVG File export to PNG Wait for the format of the picture ( The size of the thumbnail or the original )
/// </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"; // Default 64 Pixel size
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(" Incorrect size entered , Must be a number ( Groups are separated by commas );\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);// Directly append to the end of the file , Line break
}
}
}
}
}
}

版权声明
本文为[Dake mountain man]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230625378714.html
边栏推荐
猜你喜欢

Understanding the Role of Individual Units in a Deep Neural Networks(了解各个卷积核在神经网络中的作用)

Use of command line parameter passing library argparse

Unity get real geographic map application terrain notes

Install and configure Taobao image NPM (cnpm)

企业微信免登录跳转自建应用

Shapley Explanation Networks

How to present your digital portfolio: suggestions from creative recruiters

SAP自建表log功能开启

如何展示您的数字作品集:来自创意招聘人员的建议

Unity获取真实地理地图应用Terrain笔记
随机推荐
Unity 获取一个资源被那些资源引用
Mongodb 启动警告信息处理
FUEL: Fast UAV Exploration using Incremental Frontier Structure and Hierarchical Planning
Dropping Pixels for Adversarial Robustness
Mongodb starts warning information processing
C operation registry full introduction
Personality charm of high paid it workers
Unity gets the resources that a file depends on
Scrapy 修改爬虫结束时统计数据中的时间为当前系统时间
Houdini > variable building roads, learning process notes
将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)
Suggestions on university learning route planning
Towords Open World Object Detection
Nodejs (four) character reading
读取修改resource文件夹下的json文件
自己封装unity的Debug函数
Robust and Efficient Quadrotor Trajectory Generation for Fast Autonomous Flight
Apache Hudi 如何加速传统的批处理模式?
C#使用拉依达准则(3σ准则)剔除异常数据(.Net剔除一组数据中的奇异值)
关于unity获取真实地理地图转3D化的相关链接