简体   繁体   中英

Exception when converting svg to png using SVG Rendering Engine

I am trying to convert svg to png nin my asp.net applictaion using c#. I got some help from here . I followed Anish's suggestion . But I am getting exception on this. I dont have idea of this . My code include :

            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);

I am getting following exception on this:

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 

I really need help on this. Any other alternative way would be helpful too. Thanks All

You can do in js also, highcharts accepts two data format JSON and SVG. you need to add exporting highchart js reference to your app and do it in js.

Here is a link for overview http://www.highcharts.com/docs/export-module/export-module-overview

I have to added this in answer because i need 50 repo for comment and i am new to stack overflow.

the exception you are receiving is not happening on the conversion, but you get a null pointer exception that I assume happens when you try to create the stream.

var stream = new MemoryStream(byteArray);

your variable is null, and then you try to draw a bitmap out of the SvgDocument which gets your stream that is null.

You need to check why your MemoryStream is null/byteArray is invalid

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM