Save Bitmap by file name - CSharp System.Drawing

CSharp examples for System.Drawing:Image Convert

Description

Save Bitmap by file name

Demo Code


using System.Drawing.Imaging;
using System.Drawing;
using System.Collections;
using System;//from  w ww  . j  a  v  a  2s. c  om

public class Main{
        public static void Save(Bitmap bmp, string filename)
        {
            System.IO.FileInfo fi = new System.IO.FileInfo(filename);
            ImageCodecInfo codec = GetEncoderInfo(fi.Extension);
            bmp.Save(filename, codec, null);
        }
    public static ImageCodecInfo GetEncoderInfo(string format)
      {
         ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
            format = format.ToLower();
            if (format.StartsWith("."))
                format = format.Remove(0, 1);
            if (format == "jpg")
                format = "jpeg";
            else if (format == "tif")
                format = "tiff";

         for(int j = 0; j < encoders.Length; ++j)
         {
            if(encoders[j].FormatDescription.ToLower() == format)
               return encoders[j];
         }
         return null;
      }
}

Related Tutorials