How to create an EMF file from a MS Chart graph using a Stream


I am trying to create an EMF image from a MS Chart graph. I have got no problem using a FileStream to temporary store the image like this:

FileStream fileStream = new FileStream("test.emf", FileMode.Create);
chart1.SaveImage(fileStream, ChartImageFormat.EmfPlus);
Image emf = Metafile.FromFile("test.emf");

But if I try to get rid of the file using a MemoryStream I get a System.ArgumentException HResult=0x80070057 (invalid parameter).

MemoryStream memoryStream = new MemoryStream();
chart1.SaveImage(memoryStream, ChartImageFormat.EmfPlus);
Image emf = Metafile.FromStream(memoryStream);

I can't figure out why it is not working. I've read that the problem may be caused by the need to dispose the Graphics object. How can do it?

asked on Stack Overflow Mar 3, 2021 by stenio • edited Mar 3, 2021 by stenio

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0