Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

How to convert BMP to GIF in C#

4.83/5 (22 votes)
19 Dec 2010CPOL 65.2K  
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.

C#
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);
}


C#
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;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)