Click here to Skip to main content
15,881,248 members
Articles / Programming Languages / C#
Tip/Trick

How to convert BMP to GIF in C#

Rate me:
Please Sign up or sign in to vote.
4.83/5 (22 votes)
19 Dec 2010CPOL 62.8K   23   12
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)


Written By
Architect Marwin Cassovia Soft
Slovakia Slovakia
My name is Robert Kanasz and I have been working with ASP.NET, WinForms and C# for several years.
MCSD - Web Applications
MCSE - Data Platform
MCPD - ASP.NET Developer 3.5
- Web Developer 4
MCITP - Database Administrator 2008
- Database Developer 2008
MCSA - SQL Server 2012
MCTS - .NET Framework 3.5, ASP.NET Applications
- SQL Server 2008, Database Development
- SQL Server 2008, Implementation and Maintenance
- .NET Framework 4, Data Access
- .NET Framework 4, Service Communication Applications
- .NET Framework 4, Web Applications
MS - Programming in HTML5 with JavaScript and CSS3 Specialist

Open source projects: DBScripter - Library for scripting SQL Server database objects


Please, do not forget vote

Comments and Discussions

 
GeneralReason for my vote of 1 No color mapping... Pin
lolocmwa8-Dec-10 3:48
lolocmwa8-Dec-10 3:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.