首页 > 其他分享 >将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)

将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)

时间:2023-02-27 21:04:32浏览次数:41  
标签:原图 string 缩略图 Windows SVG System savePath sz using


WPF的XAML文档(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="尺寸" 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代码:(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>
/// 将指定路径下的所有SVG文件导出成PNG等格式的图片(缩略图或原图大小)
/// </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"; //默认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");
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);// 直接追加文件末尾,换行
}
}
}
}
}
}


标签:原图,string,缩略图,Windows,SVG,System,savePath,sz,using
From: https://blog.51cto.com/JohnsonJu/6089165

相关文章

  • Xamarin版的C# SVG路径解析器
    Xamarin版的C#SVG路径解析器,对SVG的Path路径进行解析,其中包括:主程序SvgPathParser.cs,相关接口定义:ISourceFormatter.cs,辅助类:FormatterRocks.cs,从接口派生的CSharpCo......
  • svg symbol模板
    svgsymbol<body><!--使用svg--><svgclass="svg-icontext-blue-500"style="font-size:5rem"aria-hidden="true"><usexlink:href="#icon-photo"fill/></......
  • 用Java给您的图片瘦身之Thumbnailator技术(缩略图)
    https://blog.csdn.net/yelangkingwuzuhu/article/details/127151913  packagecom.huaze.form.util;importlombok.SneakyThrows;importnet.coobird.thumbnailator.......
  • Android预览超长图原图并且可以滑动查看超长图
    概述:根据需求方的要求,app中预览大图需要显示高清图片。且支持高清预览横向长图(原图)和纵向长图(原图)。且图片可以自由的放大缩小,并且超长图默认需要撑满横屏或者竖屏,可以......
  • PDF中嵌入SVG图——reportlab生成pdf
    由于直接使用reportlab绘制图表不是很方便,于是想到用matplotlib画图,将SVG嵌入PDF的方法。需要用到svglib这个库,可以用pipinstallsvglib安装一个完整的例子:fromioi......
  • svg矢量二维码加盖在PDF文件中
    正常行驶的bitmap类型的二维码格式,加载到PDF中,将会导致二维码失真,无法扫描。矢量图可根据尺寸大小进行调节,不会出现失真模糊情况所用依赖<PackageReferenceInclude="Fr......
  • 数据增强时对原图和分割图做相同的变换
    方法一:使用torch.manual_seed(17)固定随机数种子,再用torchvision.transforms做变换但是这种方法有时候会失效,可能是transforms函数不能在别的py文件中写好调用到本文件,需......
  • flag-icons | 世界所有国家的国旗 SVG 图标
    Boss有话说flag-icons 全球所有国家的国旗SVG格式素材,附带CSS代码以及国旗图标使用教程,分成亚洲、非洲、欧洲、北美洲、南美洲、大洋洲的所有国旗都可以在这里看到,还......
  • styled-components给svg添加动画
    ​​https://stackoverflow.com/questions/68043891/inherited-svg-functional-component-cannot-add-style-styled-components​​如下直接继承react组件然后添加样式居......
  • SVG animateMotion All In One
    SVGanimateMotionAllInOneTheSVG<animateMotion>elementprovidesawaytodefinehowanelementmovesalongamotionpath.demos<svgviewBox="002001......