以编程方式将svg转换为图像

我试图将我的svg转换为图像我一直在寻找几个工具,但仍然无法使这个hapen 🙁

1. SVG渲染引擎,但由于它没有文档,我遇到了麻烦

这是我的代码:

using (FileStream fileStream = File.OpenRead(@"C:\sample.svg")) { MemoryStream memoryStream = new MemoryStream(); memoryStream.SetLength(fileStream.Length); fileStream.Read(memoryStream.GetBuffer(), 0, (int)fileStream.Length); SvgDocument svgDocument = SvgDocument.Open(memoryStream); string imagePath = "local.jpg"; svgDocument.Draw().Save(imagePath); } 

或这个:

  var sampleDoc = SvgDocument.Open(@"C:\sample.svg"); sampleDoc.Draw().Save(@"C:\sample.png"); 

既不起作用!

我收到错误:

 Object reference not set to an instance of an object. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.NullReferenceException: Object reference not set to an instance of an object. Source Error: Line 363: SvgDocument svgDocument = SvgDocument.Open(memoryStream); Line 364: string imagePath = "local.jpg"; Line 365: svgDocument.Draw().Save(imagePath); Line 366: } Line 367: 

问题似乎是svgDocument为null!

2. Inkscape

我的代码:

  string svgFileName = HttpContext.Current.Server.MapPath("sample.svg"); string PngRelativeDirectory = HttpContext.Current.Server.MapPath("~/"); string pngName = "svgpieresult.png"; string pngFileName = HttpContext.Current.Server.MapPath("pngName); // ignored assume sample.svg is in the web app directory //using (StreamWriter writer = new StreamWriter(svgFileName, false)) //{ // writer.Write(svgXml); //} string inkscapeArgs = string.Format(@"-f ""{0}"" -e ""{1}""", svgFileName, pngFileName); Process inkscape = Process.Start( new ProcessStartInfo("C:\\Program Files\\inkscape\\inkscape.exe", inkscapeArgs)); inkscape.WaitForExit(3000); //Context.RewritePath(pngName); context.Response.Redirect(PngRelativeDirectory + pngName); 

但什么都没发生!! 我想我缺少一些东西来保存图像而不是RewritePath()

如果你有一个更好的解决方案,我会发布一个关于如何使用它的例子,谢谢

这是我的svg:

   JanuaryFebruaryMarchAprilMayJuneJanuaryFebruaryMarchAprilMayJune  

我在这里对你的代码进行了一些修改:我认为InkScape对你的路径有问题,你已经习惯了

PngRelativeDirectory +“\”+ pngFileName

PngRelativeDirectory中已经有一个“\”,所以路径来了我们的c:\\ sample.png

我还将Context.RewritePath更改为Response.Redirect – 我在浏览器中获取了在浏览器中呈现的饼图。

 string svgFileName = HttpContext.Current.Server.MapPath("sample.svg"); string PngRelativeDirectory = "C:\\"; string pngName = "svgpieresult.png"; string pngFileName = HttpContext.Current.Server.MapPath(pngName); /* ignored assume sample.svg is in the web app directory using (StreamWriter writer = new StreamWriter(svgFileName, false)) { writer.Write(svgXml); } */ string inkscapeArgs = string.Format(@"-f ""{0}"" -e ""{1}""", svgFileName, pngFileName); Process inkscape = Process.Start( new ProcessStartInfo( "C:\\Program Files\\inkscape\\inkscape.exe", inkscapeArgs)); inkscape.WaitForExit(3000); //Context.RewritePath(pngName); this.Response.Redirect(pngName); 

您的SVG在Chrome和渲染中加载。

我已从CodePlex下载了您的SVG和DLL,并在LinqPad4中使用您的SVG重新创建了简单的两行失败。

我得到同样的失败,但LinqPad4显示DLL跟踪输出,它显示

 ... LINQPad.exe Information: 0 : Begin CreateElement: rect LINQPad.exe Information: 0 : Begin SetAttributes LINQPad.exe Information: 0 : End SetAttributes LINQPad.exe Information: 0 : End CreateElement LINQPad.exe Information: 0 : End Read loaded -- I added this to show the different method calls LINQPad.exe Information: 0 : Begin Render ... StackTrace at Svg.SvgUnit.ToDeviceValue(ISvgStylable styleOwner, Boolean vertical) at Svg.SvgRectangle.get_Path() at Svg.SvgGraphicsElement.Render(SvgRenderer renderer) at Svg.SvgRectangle.Render(SvgRenderer renderer) at Svg.SvgElement.RenderChildren(SvgRenderer renderer) at Svg.SvgElement.Render(SvgRenderer renderer) at Svg.SvgDocument.Draw() HelpLink null 

所以当它试图绘制第一条路径时会出现问题。

你说没有文档,但CodePlex上有整个源代码,包括项目文件等。 您可以下载它,看看是否可以在Svg.SvgUnit.ToDeviceValue方法中解决问题…

…或者只是下载ImageMagick并使用Process类来触发命令行程序Convert.exe