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.
Unfortunately, activation email could not send to your email. Please try again.

Chart conversion to bytes

Thread ID:

Created:

Updated:

Platform:

Replies:

126440 Oct 14,2016 02:19 PM Oct 19,2016 05:54 AM Windows Forms 7
loading
Tags: Chart
Nicolas
Asked On October 14, 2016 02:19 PM

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

Anandaraj T [Syncfusion]
Replied On October 17, 2016 12:56 AM

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

Nicolas
Replied On October 17, 2016 02:15 AM

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

Anandaraj T [Syncfusion]
Replied On October 17, 2016 02:55 AM

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 


Nicolas
Replied On October 17, 2016 03:54 AM

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

Anandaraj T [Syncfusion]
Replied On October 18, 2016 04:49 AM

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 


Nicolas
Replied On October 18, 2016 04:16 PM

All good now, thanks! - Nicolas

Preethi Nesakkan Gnanadurai [Syncfusion]
Replied On October 19, 2016 05:54 AM

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


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;