【问题标题】:Exception when converting svg to png using SVG Rendering Engine使用 SVG 渲染引擎将 svg 转换为 png 时出现异常
【发布时间】:2015-02-26 21:36:17
【问题描述】:

我正在尝试使用 c# 在我的 asp.net 应用程序中将 svg 转换为 png。我从here 那里得到了一些帮助。我听从了 Anish 的建议。但我对此有所例外。我不知道这个。我的代码包括:

            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 

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

【问题讨论】:

    标签: javascript c# svg


    【解决方案1】:

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

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

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

    【讨论】:

    • 我已经在 svg 中完成了图表。现在我只想将其转换为图像以进行导出。
    • 您可以使用phantom.js创建图像并存储在服务器上,之后您可以从那里导出。Phantom.js用于在highcharts中创建服务器端的图像。这是一个参考链接highcharts.com/component/content/article/2-articles/news/…
    【解决方案2】:

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

    var stream = new MemoryStream(byteArray);
    

    您的变量为空,然后您尝试从 SvgDocument 中绘制一个位图,从而使您的流为空。

    您需要检查为什么您的 MemoryStream 为 null/byteArray 无效

    【讨论】:

      猜你喜欢
      • 2012-04-24
      • 2015-10-04
      • 2015-04-06
      • 1970-01-01
      • 2012-10-18
      • 1970-01-01
      • 2022-01-15
      • 2015-11-20
      • 2018-09-07
      相关资源
      最近更新 更多