首页 > 其他分享 >SVG中Path Data数据简化及文件夹所有文件批量导出为图片

SVG中Path Data数据简化及文件夹所有文件批量导出为图片

时间:2023-02-27 21:04:44浏览次数:48  
标签:StreamWriter string SVG System str IO using Path 文件批量


先看效果图片:

SVG中Path Data数据简化及文件夹所有文件批量导出为图片_SVG

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>
/// 将SVG批量导出成PNG等图片
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
//保存图片路径
string savePath = @"d:\output";
//简化矢量图输入路径
string savePathNew = @"d:\outputNew";
//SVG文件路径
string svgFilePath = @"d:\outputNew";
//string svgFilePath = @"E:\界面\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("输入的尺寸有误,必须为数字(多组用逗号隔开);\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);// 直接追加文件末尾,换行
}
}
}
MessageBox.Show(string.Format("有{0}张矢量标志图片,共生成{1}张图片!", icoCount, icoAllCount));
} /// <summary>
/// 根据菜单名称建立空SVG内容的文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnCreateSVG_Click(object sender, RoutedEventArgs e)
{
string menuNames = @"成品类型维护|成品维护";
string contentTemplate = @"<svg version=""1.1"" id=""图层_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);// 直接追加文件末尾,换行
}
}
} 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 = @"系统管理|部门维护";
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);// 直接追加文件末尾,换行
}
}
}
} 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);// 直接追加文件末尾,换行
}
}
catch (Exception exc)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(savePathNew + @"\error.txt", true))
{
sw.WriteLine(fileName);// 直接追加文件末尾,换行
}
}
}
} /// <summary>
/// SVG代码简化
/// </summary>
/// <param name="str">SVG代码</param>
/// <returns>简化后的代码</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]);// 直接追加文件末尾,换行
//}
} result = str;
}
else { result = str; } return result.Replace("http://www.w3org/", "http://www.w3.org/");
}
}
}


标签:StreamWriter,string,SVG,System,str,IO,using,Path,文件批量
From: https://blog.51cto.com/JohnsonJu/6089164

相关文章

  • 将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)
    WPF的XAML文档(Main.xaml):<Windowx:Class="SVG2Image.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas......
  • Xamarin版的C# SVG路径解析器
    Xamarin版的C#SVG路径解析器,对SVG的Path路径进行解析,其中包括:主程序SvgPathParser.cs,相关接口定义:ISourceFormatter.cs,辅助类:FormatterRocks.cs,从接口派生的CSharpCo......
  • Absolute Path Traversal 错误解决
    AbsolutePathTraversal(APT)是一种常见的安全漏洞,攻击者可以通过该漏洞访问应用程序的文件系统中的文件,包括敏感信息,从而可能导致应用程序遭受攻击。一、使用专门的......
  • The bean ‘api‘, defined in class path resource [com/common/swagger/SwaggerAuto
    Thebean‘api‘,definedinclasspathresource[com/common/swagger/SwaggerAutoConf1.解决办法-1将重复的bean覆盖掉加一个这个注解spring.main.allow-bean-definit......
  • svg symbol模板
    svgsymbol<body><!--使用svg--><svgclass="svg-icontext-blue-500"style="font-size:5rem"aria-hidden="true"><usexlink:href="#icon-photo"fill/></......
  • Day 11 11.1 Xpath解析
    xpath解析xpath在Python的爬虫学习中,起着举足轻重的地位,对比正则表达式re两者可以完成同样的工作,实现的功能也差不多,但xpath明显比re具有优势,在网页分析上使re退居二线。......
  • CF845G - Shortest Path Problem?
    题意:求带边权无向图上\(1\)到\(n\)的异或最短路,可以重复经过某条边。首先,我们考虑从\(x\)到\(y\)的路径\(A\),它的权值是\(a\)。我们从路径中途的某个地方离开路......
  • inux配置PATH路径
    查看PATH:echo$PATH以添加python3为列修改方法一:exportPATH=PATH:PATH:PATH:HOME/bin:exportPATH=PATH:PATH:PATH:HOME/bin:/usr/local/python3/bin//配置完......
  • @PathVariable相关注意点
    1、使用@PathVariable注解时,说明url路径采用restful风格,即http://localhost:8080/zgbj/1/22、@PathVariable注解也可以接受多个参数,例如publicModelAndViewconfigAntenn......
  • (我自己总结的最完美的vue和nginx的代理解析)vue的proxy和pathRewrite和nginx的代理的解
    vue编译器和nginx都能启动我们前端项目的,二者都能对前端的请求进行转发代理到后端项目中我们本地电脑也可以下载一个nginx启动我们的前端项目,而不用通过vue编译器启动。......