当前位置:网站首页>Simplify exporting to SVG data files and all images in SVG folder
Simplify exporting to SVG data files and all images in SVG folder
2022-04-23 07:54:00 【Dake mountain man】
First look at the effect picture :
using Svg;
using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows;
namespace SVG2Image
{
/// <summary>
/// take SVG Batch export to PNG Wait for the picture
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
// Save image path
string savePath = @"d:\output";
// Simplify vector graph input path
string savePathNew = @"d:\outputNew";
//SVG File path
string svgFilePath = @"d:\outputNew";
//string svgFilePath = @"E:\ Interface \POD0606New\ICO";
private void button_Click(object sender, RoutedEventArgs e)
{
string sizeText = textBox.Text;
if (string.IsNullOrEmpty(sizeText)) sizeText = "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(" 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");
int icoCount = arraySvgFiles.Length;
int icoAllCount = 0;
foreach (string file in arraySvgFiles)
{
string fileName = System.IO.Path.GetFileNameWithoutExtension(file);
fileName = fileName.Replace("_New", "");
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);
}
icoAllCount++;
}
}
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
}
}
}
MessageBox.Show(string.Format(" Yes {0} Vector logo picture , A total of generated {1} A picture !", icoCount, icoAllCount));
}
/// <summary>
/// Create an empty menu based on the menu name SVG The content of the document
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCreateSVG_Click(object sender, RoutedEventArgs e)
{
string menuNames = @" Finished product type maintenance | Finished product maintenance ";
string contentTemplate = @"<svg version=""1.1"" id="" Layers _1"" xmlns=""http://www.w3.org/2000/svg"" xmlns:xlink=""http://www.w3.org/1999/xlink"" x=""0px"" y=""0px""
width=""648.992px"" height=""648.992px"" viewBox=""0 0 648.992 648.992"" enable-background=""new 0 0 648.992 648.992""
xml:space=""preserve"">
</svg>";
string[] arrayMenus = menuNames.Split('|');
foreach (string menu in arrayMenus)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(savePath + @"\" + menu + @".svg", true))
{
sw.WriteLine(contentTemplate);// Directly append to the end of the file , Line break
}
}
}
private void btnLostSvg_Click(object sender, RoutedEventArgs e)
{
string[] arraySvgFiles = Directory.GetFiles(svgFilePath, "*.svg");
List<string> listSvgFiles = new List<string>();
foreach (string file in arraySvgFiles)
{
listSvgFiles.Add(System.IO.Path.GetFileNameWithoutExtension(file));
}
string menuNames = @" System management | Department maintenance ";
string[] arrayMenus = menuNames.Split('|');
foreach (string menu in arrayMenus)
{
if (!listSvgFiles.Contains(menu))
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(savePath + @"\WithoutFile.txt", true))
{
sw.WriteLine(menu);// Directly append to the end of the file , Line break
}
}
}
}
private void btnReplace_Click(object sender, RoutedEventArgs e)
{
//string oldContent = txtBoxOldContent.Text;
//txtBoxResultContent.Text = GetNumberByRound(oldContent);
if (!Directory.Exists(savePathNew))
{
Directory.CreateDirectory(savePathNew);
}
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);
svgFileContents = GetNumberByRound(svgFileContents);
string saveFileName = savePathNew + @"\" + fileName + @"_New" + System.IO.Path.GetExtension(file);
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(saveFileName, false))
{
sw.WriteLine(svgFileContents);// Directly append to the end of the file , Line break
}
}
catch (Exception exc)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(savePathNew + @"\error.txt", true))
{
sw.WriteLine(fileName);// Directly append to the end of the file , Line break
}
}
}
}
/// <summary>
/// SVG Code simplification
/// </summary>
/// <param name="str">SVG Code </param>
/// <returns> Simplified code </returns>
private string GetNumberByRound(string str)
{
string pattern = @"([-,a-zA-Z\s]*)(\d+\.?\d*)";
Regex r = new Regex(pattern);
str = Regex.Replace(str, @"<!--.*-->", "");
str = Regex.Replace(str, @"<!DOCTYPE.*>", "");
/*
<!-- Generator: Adobe Illustrator 14.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 43363) -->
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">
*/
str = Regex.Replace(str, @"\r\n", "");
string result = string.Empty;
if (r.IsMatch(str))
{
MatchCollection mc = r.Matches(str);
if (mc.Count == 0) return result;
Dictionary<string, string> dicNum = new Dictionary<string, string>();
foreach (Match m in mc)
{
GroupCollection colls = Regex.Match(m.Value, pattern).Groups;
string v = string.Empty;
if (colls.Count > 1)
{
//foreach (Group item in colls)
{
//Console.WriteLine(item.Value.ToString());
string k = colls[0].Value;
v = colls[2].Value;
if (!string.IsNullOrEmpty(v) && !dicNum.ContainsKey(k))
{
dicNum.Add(k, colls[1].Value + Math.Round(Convert.ToDecimal(v), 1, MidpointRounding.AwayFromZero).ToString());
}
}
}
else
{
v = m.Value;
if (!string.IsNullOrEmpty(v) && !dicNum.ContainsKey(v))
{
dicNum.Add(v, Math.Round(Convert.ToDecimal(v), 1, MidpointRounding.AwayFromZero).ToString());
}
}
}
foreach (string key in dicNum.Keys)
{
str = str.Replace(key, dicNum[key]);
//using (System.IO.StreamWriter sw = new System.IO.StreamWriter(savePath + @"\Keys.txt", true))
//{
// sw.WriteLine(key +"\t" + dicNum[key]);// Directly append to the end of the file , Line break
//}
}
result = str;
}
else { result = str; }
return result.Replace("http://www.w3org/", "http://www.w3.org/");
}
}
}

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

C# 多个矩形围成的多边形标注位置的问题

STO With Billing 跨公司库存转储退货

SAP TR手动导入系统操作手册

H5 local storage data sessionstorage, localstorage

利用Lambda表达式解决c#文件名排序问题(是100大还是11大的问题)

Scrapy modifies the time in the statistics at the end of the crawler as the current system time

【NLP笔记】CRF原理初探

How to present your digital portfolio: suggestions from creative recruiters

Houdini>流体,刚体导出学习过程笔记

ABAP ALV显示金额与导出金额不一致
随机推荐
About unity to obtain links related to the transformation of real geographic maps into 3D
Read and modify the JSON file under the resource folder
Teach-Repeat-Replan: A Complete and Robust System for Aggressive Flight in Complex Environments
'NPM' is not an internal or external command, nor is it a runnable program or batch file
自己封装unity的Debug函数
SAP TR手动导入系统操作手册
SVG中年月日相关的表达式
Dropping Pixels for Adversarial Robustness
Unity get real geographic map application terrain notes
SAP STO With Billing流程与配置
利用网页表格导出EXCEL表格加线框及表格内部间距的问题
Suggestions on university learning route planning
Apache Hudi 如何加速传统的批处理模式?
SampleCameraFilter
颜色转换公式大全及转换表格(31种)
Enterprise wechat login free jump self built application
Weblux file upload and download
Houdini>刚体, 刚体破碎RBD
Shapley Explanation Networks
NodeJS(四) 字符读取