当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
企业微信免登录跳转自建应用
Nodejs (II) read files synchronously and asynchronously
H5 local storage data sessionstorage, localstorage
解决在docker中部署mysql8, 密码正确但无法登陆MySQL问题
About unity to obtain links related to the transformation of real geographic maps into 3D
RGB颜色转HEX进制与单位换算
ABAP ALV显示金额与导出金额不一致
Plane definition - plane equation
Unable to process jar entry [module info. Class]
Window10版MySQL设置远程访问权限后不起效果
UnityShader基础
SQL sorts string numbers
Electronic builder package error: proxyconnect TCP: Dial TCP: 0: connectex
Mongodb 启动警告信息处理
NodeJS(二)同步读取文件和异步读取文件
Unity获取真实地理地图应用Terrain笔记
Unityshader Foundation
Protobuf 使用
根据某一指定的表名、列名及列值来向前或向后N条查相关列值的SQL自定义标量值函数
Personality charm of high paid it workers









