使用 SVG 渲染引擎将 svg 转换为 png 时出现异常

Exception when converting svg to png using SVG Rendering Engine

本文关键字:png 异常 转换 svg SVG 引擎 使用      更新时间:2023-09-26

我正在尝试使用 c# 在我的 asp.net 应用程序中将 svg 转换为 png n。我从这里得到了一些帮助。我听从了阿尼什的建议。但我在这方面得到了例外。我不知道这个.我的代码包括:

            string path = "d:''";
            string svgstr = temp.Value;
            var byteArray = Encoding.ASCII.GetBytes(svgstr);
            var stream = new MemoryStream(byteArray);
            var bitmap = SvgDocument.Open(stream).Draw();
            bitmap.Save(path, ImageFormat.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 102:            var stream = new MemoryStream(byteArray);
Line 103:            
Line 104:            var bitmap = SvgDocument.Open(stream).Draw();
Line 105:             
Line 106:                bitmap.Save(path, ImageFormat.Png);
Source File: e:'HighchartDemo'HighchartDemo'ColumnChart.aspx.cs    Line: 104 

我真的需要这方面的帮助。任何其他替代方法也会有所帮助。谢谢大家

你也可以在js中做,highcharts接受两种数据格式的JSON和SVG。 您需要将导出 Highchart JS 引用添加到您的应用程序中并在 JS 中执行此操作。

这是概述 http://www.highcharts.com/docs/export-module/export-module-overview 的链接

我必须在答案中添加这个,因为我需要 50 个 repo 进行评论,而且我是堆栈溢出的新手。

您收到的异常在转换中没有发生,但是您会收到一个空指针异常,我认为当您尝试创建流时会发生该异常。

var stream = new MemoryStream(byteArray);

您的变量为 null,然后您尝试从 SvgDocument 中绘制一个位图,该文档将获取为空的流。

您需要检查为什么您的内存流为空/字节数组无效