Bertrand_D Posted April 21, 2009 Report Share Posted April 21, 2009 Hi, I want to export a Winform chart to a .png or .gif file format, using Metafile first (rather than bitmap) to ensure a better quality of the output image. Also, I am working with MemoryStream rather than saving files on disk. I have the following Chart Exportation crashing, could you pelase help? (it works fine when I save files to disk, not with streams) private static MemoryStream ExportChartToStream(ChartFX.WinForms.Chart chart){ MemoryStream imgStream = new MemoryStream(); MemoryStream metafileStream = new MemoryStream(); chart.Export(FileFormat.Metafile, metafileStream); var meta = new Metafile(metafileStream); <----------- Generic Error occured in GDI+ ... meta.Save(imgStream, ImageFormat.Png); return imgStream;}Thank you, Bertrand Quote Link to comment Share on other sites More sharing options...
AndreG Posted April 22, 2009 Report Share Posted April 22, 2009 I dont have an answer to your question, but I might have a better way if all you are trying to do is export charts in higher quality. Try the following: int desiredDPI = 300; Rectangle r = new Rectangle(0, 0, (chart1.Width * desiredDPI) / 96, (chart1.Height * desiredDPI) / 96); Bitmap bmp = new Bitmap (r.Width, r.Height); bmp.SetResolution(desiredDPI, desiredDPI); Graphics g = Graphics .FromImage(bmp); chart1.Paint(g, r, PaintStyles .Background | PaintStyles .Border | PaintStyles .Print); bmp.Save(@"c:\temp\cfx.png", ImageFormat.Png); g.Dispose(); bmp.Dispose(); Quote Link to comment Share on other sites More sharing options...
Bertrand_D Posted April 22, 2009 Author Report Share Posted April 22, 2009 Hi, Thank you for your answer.I have been trying to use your method but i am getting a smaller graph in the Rectangle with a lower definition.I am finnally using the basic following code (simplier and shorter) providing a very good quality of the chart. private MemoryStream ExportChartToStream(ChartFX.WinForms.Chart chart) { try { MemoryStream imgStream = new MemoryStream(); chart.Export(FileFormat.Bitmap, imgStream); return imgStream; } catch (Exception e) { throw new Exception ("Error in ExportChartToStream()", e); } } Thanks for your help.Bertrand Quote Link to comment Share on other sites More sharing options...
AndreG Posted April 22, 2009 Report Share Posted April 22, 2009 Great. Wonder why my code is not working for you, but thanks for sharing! Quote Link to comment Share on other sites More sharing options...
Ozzard Posted September 9, 2009 Report Share Posted September 9, 2009 A late response, I accept - but the trick is to reset the stream's position to 0 before reading from it. MemoryStream stream = new MemoryStream(); chart.Export( FileFormat.Metafile, stream);stream.Position = 0; Image metaImage = Image.FromStream(stream); stream.Close(); Quote Link to comment Share on other sites More sharing options...
Bertrand_D Posted October 29, 2009 Author Report Share Posted October 29, 2009 I have a similar issue and I just read your answer which solved the original problem. Thanks a lot for sharing it. Bertrand Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.