GIF to JPEG over white


B

bmjnine

Hi all,

I am converting image files of various formats to JPEG using:

System.Drawing.Image img = System.Drawing.Image.FromFile(myfilepath);

Works great except that transparent GIF's are over a black background
instead of white. What would be the simplest way to convert GIF's to an
Image object over a white background?

I have tried the following:

System.Drawing.Image img = new Bitmap(myfilepath);
Bitmap bmp = new Bitmap(img.Width, img.Height);
Graphics gfc = Graphics.FromImage(bmp);
gfc.Clear(Color.White);
gfc.DrawImage(img, 0, 0);
img = bmp;

This does work for the GIF's over white, but then sometimes when a JPEG
runs through this code, it gets resized, so I'm not convinced this is
the best route.

Thanks in advance,
Alyssa
 
Ad

Advertisements

?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi,

(e-mail address removed) napisa³(a):
Hi all,

I am converting image files of various formats to JPEG using:

System.Drawing.Image img = System.Drawing.Image.FromFile(myfilepath);

Works great except that transparent GIF's are over a black background
instead of white. What would be the simplest way to convert GIF's to an
Image object over a white background?

I have tried the following:

I recommend these changes in your code:

System.Drawing.Image img = new Bitmap(myfilepath);
PixelFormat pixFormat=img.PixelFormat;
if( pixFormat==PixelFormat.Format8bppIndexed
|| Image.IsAlphaPixelFormat(pixFormat) ) {
Bitmap bmp = new Bitmap(img.Width, img.Height);
using( Graphics gfc = Graphics.FromImage(bmp) ) {
gfc.Clear(Color.White);
gfc.DrawImageUnscaled(img, 0, 0);
}
Image oldImg=img;
img = bmp;
oldImg.Dispose();
}
This does work for the GIF's over white, but then sometimes when a JPEG
runs through this code, it gets resized, so I'm not convinced this is
the best route.

These changes can assure you that there's no way to draw JPEG
till it has no alpha channel.

Cheers! & HTH
Marcin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top