65.9K
CodeProject is changing. Read more.
Home

How to convert BMP to GIF in C#

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.83/5 (20 votes)

Dec 1, 2010

CPOL
viewsIcon

65193

How to convert BMP to GIF in C#

In this post, I will show you how to convert BMP file to GIF in C#. The first step you must do is to create a Bitmap object. In this example, I passed filename to constructor of this object. Next you need to create ImageCodecInfo and choose the appropriate mime type of output format. You can set quality and compression of output image. In case you need to do this, you must create EncoderParameters object which represents list of EncoderParameter objects. This objects represent set of parameters which encoder uses for image encoding.
static void Main(string[] args)
{
    Bitmap myBitmap=new Bitmap(@"test.bmp");
    ImageCodecInfo myImageCodecInfo = GetEncoderInfo("image/gif"); ;
    EncoderParameter encCompressionrParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, (long)EncoderValue.CompressionLZW); ;
    EncoderParameter encQualityParameter = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, 0L);
    EncoderParameters myEncoderParameters = new EncoderParameters(2);
    myEncoderParameters.Param[0] = encCompressionrParameter;
    myEncoderParameters.Param[1] = encQualityParameter;
    myBitmap.Save("Output.gif", myImageCodecInfo, myEncoderParameters);
}
private static ImageCodecInfo GetEncoderInfo(String mimeType)
{
    int j;
    ImageCodecInfo[] encoders;
    encoders = ImageCodecInfo.GetImageEncoders();
    for (j = 0; j < encoders.Length; ++j)
    {
        if (encoders[j].MimeType == mimeType)
            return encoders[j];
    }
    return null;
}