目前有哪些方法可以使用C#以编程方式将SVG图像转换为PNG或JPEG?
我已经阅读了有关该主题的所有现有SO问题,所有这些问题都涉及使用外部流程来启动第三方程序.就我而言,这不是一个选项,因为我们很快就会迁移到Azure.
我需要做的是从磁盘加载SVG文件,理想情况下将其转换为我可以使用System.Drawing类进行操作的东西.
有任何想法吗?
好.我将与我的解决方案共享,以将SVG文件重新调整为适当大小.
我安装这个nuget包
Install-Package Svg
Run Code Online (Sandbox Code Playgroud)
你可以在这里找到github上的软件包源代码
然后,你可以这样做:
var svgDocument = SvgDocument.Open(path);
using (var smallBitmap = svgDocument.Draw())
{
var width = smallBitmap.Width;
var height = smallBitmap.Height;
if (width != 2000)// I resize my bitmap
{
width = 2000;
height = 2000/smallBitmap.Width*height;
}
using (var bitmap = svgDocument.Draw(width, height))//I render again
{
bitmap.Save(pngPath, ImageFormat.Png);
}
}
Run Code Online (Sandbox Code Playgroud)
请享用!