0

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

Community
  • 1
  • 1
Arsalan Sherwani
  • 889
  • 2
  • 25
  • 60

2 Answers2

0

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.

  • I have already done the charting in svg. Now I just want it to be converted to image for exportation. – Arsalan Sherwani Jan 27 '15 at 15:17
  • you can use phantom.js to create image and store on server, after that you can export from there.. Phantom.js is used to create image on server side in highcharts. Here is a link for reference http://www.highcharts.com/component/content/article/2-articles/news/56-improved-image-export-with-phantomjs. – Sandeep Chadgal Jan 28 '15 at 07:35
0

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

schmendrick
  • 471
  • 1
  • 5
  • 12