Jump to content
Software FX Community

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


Delphin

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...