We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Chart conversion to bytes

Hi, I looked into all export options for the charts, and I saw nothing to convert the chart as an image or bytes via a stream. I would like to avoid the creation of an intermediatefile. Thanks in advance! - Nicolas

7 Replies

AT Anandaraj T Syncfusion Team October 17, 2016 04:56 AM UTC

Hi Nicolas, 

Thanks for using Syncfusion products. 

We can use the Draw method of chart control to draw chart in an Image or Bitmap object. We can convert this Image object to a stream or byte array as required. 

We have prepared a sample for this requirement and it can be downloaded from the following link 

Please refer the following code snippet to achieve this 

[C#] 
 
            //Draw chart in an Image or Bitmap object 
            Image img = new Bitmap(this.chartControl1.Width, this.chartControl1.Height);             
            this.chartControl1.Draw(img); 
 
            //Convert image to stream 
            Stream stream = new MemoryStream(); 
            img.Save(stream, ImageFormat.Png); 
             
            //Convert stream to bytes 
            byte[] chartBytes = new byte[stream.Length]; 
            stream.Write(chartBytes, 0, chartBytes.Length); 
 
            //For verifying the exported image. 
            img.Save("Chart.png"); 
 
            //Dispose image and stream 
            img.Dispose(); 
            stream.Dispose(); 
 

Please let us know if you have any concern. 

Regards, 
Anand


NI Nicolas October 17, 2016 06:15 AM UTC

Hi, still not working on my side, have you tried to convert back the bytes to image? My side it's returning nothing - Thanks in advance, Nicolas


AT Anandaraj T Syncfusion Team October 17, 2016 06:55 AM UTC

Hi Nicolas, 

Sorry for the inconvenience caused. 

In our previous sample, stream was not converted to bytes properly. On further analysis, we found that stream was not positioned to 0 before copying it to a byte array. We can either use Read method or ToArray method of MemoryStream to copy the stream contents to a byte array. 

Note: ToArray method is available only for MemoryStream object and not for Stream object. When using Read method, set the position of stream to 0. 

We have modified the sample and it can be downloaded from the following link 

Please refer the following code snippet to achieve this 
[C#] 
 
            //Draw chart in an Image or Bitmap object 
            Image img = new Bitmap(this.chartControl1.Width, this.chartControl1.Height);             
            this.chartControl1.Draw(img); 
 
            //Convert image to memory stream 
            MemoryStream stream = new MemoryStream(); 
            img.Save(stream, ImageFormat.Png); 
             
            //Convert memory stream to bytes 
            byte[] chartBytes = stream.ToArray(); 
 
            //For verifying the exported image. 
            stream.Dispose(); 
            img.Dispose(); 
             
            stream = new MemoryStream(chartBytes); 
            img = Image.FromStream(stream); 
            img.Save("Chart.png"); 
 
            //Dispose image and stream 
            img.Dispose(); 
            stream.Dispose(); 
 


Please let us know if you have any concern. 

Regards, 
Anand 



NI Nicolas October 17, 2016 07:54 AM UTC

Hi! Now it's working good! Thank you! One more question if you do not mind, when the render is made, apparently the OnChartControl1_ChartFormatAxisLabel which is there to apply a custom format on the label is not working. On screen, I can see the chart with the proper labels, but once streamed, those labels are replaced with the original numeric values - Thank you in advance! - Nicolas


AT Anandaraj T Syncfusion Team October 18, 2016 08:49 AM UTC

Hi Nicolas, 

Thanks for the update. 

The reported issue is fixed in the latest version of Essential Studio (Volume 3, 2016). We request you to upgrade to the latest version. 

Please let us know if you need further assistance on this. 

Regards, 
Anand 



NI Nicolas October 18, 2016 08:16 PM UTC

All good now, thanks! - Nicolas


PN Preethi Nesakkan Gnanadurai Syncfusion Team October 19, 2016 09:54 AM UTC

Hi Nicolas,  
  
Most Welcome. 
  
Please let us know if you need any further assistance. 
  
Regards,  
Preethi 


Loader.
Live Chat Icon For mobile
Up arrow icon