当前位置:网站首页>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
边栏推荐
- VBA调用SAP RFC实现数据读取&写入
- Towords Open World Object Detection
- SQL针对字符串型数字进行排序
- 【Unity VFX】VFX特效入门笔记-火花制作
- C operation registry full introduction
- Unity get real geographic map application terrain notes
- Houdini fluid > > particle fluid export to unity note
- Zhuang understand's TA notes (VI) < fakeenvreflect & rust, rust effect >
- 事件系统(二)多播事件
- Understanding the Role of Individual Units in a Deep Neural Networks(了解各个卷积核在神经网络中的作用)
猜你喜欢
基于NLP的软件安全研究(二)
Zhuang understand's TA notes (VI) < fakeenvreflect & rust, rust effect >
Houdini > variable building roads, learning process notes
Page dynamic display time (upgraded version)
Apache Hudi 如何加速传统的批处理模式?
How does Apache Hudi accelerate traditional batch mode?
Shapley Explanation Networks
对复杂字典Dictionary&lt;T1,T2&gt;排序问题
Protobuf use
将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)
随机推荐
Suggestions on university learning route planning
Nodejs (I) event driven programming
TA notes of Zhuang understand (zero) < bedding and learning methods >
Use of command line parameter passing library argparse
Towords Open World Object Detection
Protobuf 使用
Understanding the role of individual units in a deep neural networks
H5 local storage data sessionstorage, localstorage
unity 屏幕自适应
unity UGUI判断点击在UI上和3D物体上的解决方案
Teach-Repeat-Replan: A Complete and Robust System for Aggressive Flight in Complex Environments
NodeJS(一) 事件驱动编程
C#操作注册表全攻略
Page dynamic display time (upgraded version)
庄懂的TA笔记(七)<Lambert+Phong+Shadow+3EvColor+AO>
Houdini > fluid, rigid body export, learning process notes
How to present your digital portfolio: suggestions from creative recruiters
Unity C single case mode learning review notes
The page displays the current time in real time
Teach-Repeat-Replan: A Complete and Robust System for Aggressive Flight in Complex Environments