473,387 Members | 1,420 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

GIF to JPEG over white

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

Aug 29 '06 #1
1 1042
Hi,

bm*****@hotmail.com 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
Aug 30 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Ming | last post by:
Hi All, I want to write a PHP webpage which allows people to upload images (no matter what formats) to me and at the same time converts any non-jpeg image to JPEG. Here's what I have: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: Robert | last post by:
Hi I searching in other groups replay but .... :-( Wy my code (print ("<P> <IMG
21
by: cman | last post by:
does anyone know why i can't generate images with: header("Content-type:image/jpeg"); imagejpeg($img_number); i've tried different examples but i always get a text output as if the header...
5
by: TheGanjaMan | last post by:
Hi everyone, I'm trying to write up a simple image stamper application that stamps the Exif date information from the jpegs that I've taken from my digital camera and saves the new file with the...
1
by: bmjnine | last post by:
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...
0
by: Jack Wu | last post by:
Hi I've spent a good majority of my day trying to figure out how to have PIL 1.1.5 working on my OSX 10.3.9_PPC machine. I'm still stuck and I have not gotten anywhere. Could somebody please...
4
by: Greg | last post by:
I've searched everywhere for a solution Microsoft .NET Framework V 2.0.50727 AutoEventWireup="false" image2.aspx resize an image on the fly but Page_Load is triggered twice: Any suggestion?...
1
by: R.A.F. | last post by:
Hi, I have a splash form which has a background JPEG image (back mainly with some blue and white logo). on this form i want to display a text which will be updated dynamically to reflect the...
0
by: =?Utf-8?B?TWFyazEyMw==?= | last post by:
I have two pieces of VB.NET code below: a) Sends an email b) Returns a programmatically created jpeg image to a webpage. How can I embed the programmatically created jpeg into an HTML...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.