Get Encoder Info by format - CSharp System.Drawing

CSharp examples for System.Drawing:Bitmap

Description

Get Encoder Info by format

Demo Code


using System.Drawing.Imaging;
using System.Drawing;
using System.Collections;
using System;/*ww w . j  a  va  2 s .c  o m*/

public class Main{
    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