Saturday, 29 December 2012 13:56

how to convert bmp file to gif by csharp

Written by 
Rate this item
(0 votes)

to convert bitmap image file to gif file you
have to create bitmap object first.

for this you pass the bmp image filename to the
constructor of this object.then you have to
create ImageCodecInfo and select mime type for
output format.to set the quality and compression
of the output image,you must make Encode
parameters object representing the list for
Encoderparamter objects.for encoding,image
encoder uses a set of parameters represented
by the object.

the code is shown below -

static void Main(string[] args)
{
Bitmap bitmapImage=new Bitmap(@"bitmap.bmp");
ImageCodecInfo testImageCoInfo =
GetEncoderInfo("image/gif"); ;
EncoderParameter encCompressParameter =
new EncoderParameter(System.Drawing.Imaging.
Encoder.Compression, (long)
EncoderValue.CompressionLZW); ;
EncoderParameter encQuaParam = new
EncoderParameter(System.Drawing.Imaging.Encoder.
Quality, 0L);EncoderParameters testEncoderParam
= new EncoderParameters(2);testEncoderParam.Param[0]
=encCompressParameter;
testEncoderParam.Param[1] = encQuaParam;
myBitmap.Save("output_image.gif", testImageCoInfo,
testEncoderParam);
}

the funtion used is -

private static ImageCodecInfo GetEncoderInfo(String mimeTypedef)
{
int conter;
ImageCodecInfo[] encoderObj;
encoderObj = ImageCodecInfo.GetImageEncoders();
for (counter = 0; counter < encoders.Length; ++counter)
{
if (encoders[counter].MimeType == mimeTypedef)
return encoderObj[counter];
}
return null;
}

Read 1142 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.
Login to post comments