Jump to content
Software FX Community

Save chart into Png, Gif or Jpeg image on hard disk


Recommended Posts

You can use a such code, hope it will help someone. 

using System;using System.IO;using System.Drawing;using System.Drawing.Imaging;using ChartFX.WinForms;namespace ChartFxTools{   public enum eImageFormat   {   Jpeg, Gif, Png   }   public static class CChartFxTools   {   public static void Save(Chart pChart, string pImagePath, eImageFormat pImageFormat)   {   string bmpFilePath = Path.ChangeExtension(pImagePath, ".bmp");   pChart.Export(FileFormat.Bitmap, bmpFilePath);   Convert(bmpFilePath, pImageFormat);   }   private static void Convert(string pBmpFile, eImageFormat pImageFormat)   {   if (pBmpFile == null)   {   throw new ArgumentNullException("The BmpFile parameter cannot be null");   }   if (!File.Exists(pBmpFile))   {   throw new FileNotFoundException("Cannot find the " + pBmpFile + " file");   }   Bitmap bitmap = null;   ImageFormat imageFormat = GetImageFormat(pImageFormat);   try   {   bitmap = new Bitmap(pBmpFile);   string newFilePath = Path.ChangeExtension(pBmpFile, pImageFormat.ToString().ToLower());   bitmap.Save(newFilePath, imageFormat);   }   catch (System.Exception)   {   throw;   }   finally   {   if (bitmap != null)   {   bitmap.Dispose();   bitmap = null;   }   try   {   File.Delete(pBmpFile);   }   catch (System.Exception)   {   }   }   }   private static ImageFormat GetImageFormat(eImageFormat pImageFormat)   {   switch (pImageFormat)   {   case eImageFormat.Jpeg:   return ImageFormat.Jpeg;   case eImageFormat.Gif:   return ImageFormat.Gif;   case eImageFormat.Png:   return ImageFormat.Png;   }   return ImageFormat.Png;   }   }}

To use it: 

using ChartFxTools;

Chart chart = new Chart()...CChartFxTools.Save(chart, @"C:\myChartImage.png", eImageFormat.Png);...Note : do not forget to add all required references to use this source.

Link to comment
Share on other sites

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.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...