ArgumentException is thrown when trying to create an Image from the stream produced by ImageExporter


https://www.nevron.com/Forum/Topic11860.aspx
Print Topic | Close Window

By Irina - 8 Years Ago
Hello,
I need to export a chart control to EMF image without saving it to a file. The following code fails with System.ArgumentException when Image.FromStream is called.

var memoryStream = new MemoryStream();
nChartControl1.ImageExporter.SaveToStream(memoryStream, new NSize(500, 500), NResolution.ScreenResolution,  new NEmfImageFormat());
var image = Image.FromStream(memoryStream);

What is the cause of the exception and how it can be prevented?
Thank you

By Nevron Support - 8 Years Ago
Hi Irina,

You need to move the current position in the stream to its begin position (instead of the end) and it should be OK:

   var memoryStream = new MemoryStream();
    nChartControl1.ImageExporter.SaveToStream(memoryStream, new NSize(500, 500), NResolution.ScreenResolution, new NEmfImageFormat());

    memoryStream.Seek(0, SeekOrigin.Begin);
    var image = Image.FromStream(memoryStream);
By Irina - 8 Years Ago
The suggested approach works.Thank you!