C# .svg 文件到 System.Drawing.Image

C# .svg file to System.Drawing.Image

我需要将选定的 .svg 文件转换为 System.Drawing.Image 对象,以便调整大小并将其另存为 .png。谁能帮我解决这个问题?

这是我目前的情况:

Svg.SvgDocument svgDocument = SVGParser.GetSvgDocument(mPath);
image = svgDocument.Draw();

但是它给我内存不足的错误。

所以,

SVGParser.MaximumSize = new System.Drawing.Size(4000, 4000);
svgDocument = SVGParser.GetSvgDocument(mPath);
var bitmap = svgDocument.Draw();
image = bitmap;

您可以使用 SVG Rendering Engine Lib:

Install-Package Svg

用它画图非常简单:

var svgDoc = SvgDocument.Open(imagePath);

using(var Image = new Bitmap(svgDoc.Draw()))
{
    Image.Save(context.Response.OutputStream, ImageFormat.Png);
    context.Response.ContentType = "image/png";
    context.Response.Cache.SetCacheability(HttpCacheability.Public);
    context.Response.Cache.SetExpires(DateTime.Now.AddMonths(1));
}

在此示例中,我使用处理程序在浏览器上显示图像,但您只需更改 Save 方法的第一个参数即可轻松将其保存在某个文件夹中。

Miljan Vulovic 使用的资源是 svg (https://archive.codeplex.com/?p=svg)。

Link 有效期至 2021 年 7 月,届时可能会在 GitHub 上可用,但我不确定。

无论如何他的解决方案对我有用。