0

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);
fileStream.Close();
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?

stenio
  • 297
  • 1
  • 10
  • 2
    After writing to memory stream you must set position to zero before reading. – jdweng Mar 03 '21 at 10:53
  • @jdweng Yes! I tried calling Flush() but didn't think to reposition the current position at the beginning. Thanks a lot! – stenio Mar 03 '21 at 10:59

0 Answers0