I have a bitmap object and draw some curves on it by setpixel method. when I save this bitmap as jpg file, the background of my picture is not a white surface. the background is transparent. what is the problem? How can I resolve this problem?
+6
A:
Call Graphics.Clear(Color.White)
before you draw on the bitmap. If you do not already have an instance of System.Drawing.Graphics
for your bitmap, here's how to get one:
Graphics g = Graphics.FromImage(bitmap);
Clear the bitmap:
g.Clear(Color.White);
And of course, don't forget to call Dispose()
when you are done with the graphics.
g.Dispose();
Zach Johnson
2010-02-24 05:10:05
A:
Are you certain that you are saving the image out in JPEG format? AFAIK, JPEG doesn't support transparancy, so perhaps you're saving the image out as GIF or PNG with a ".jpg" extension, and your viewer is ignoring the extension.
In any case, Zach's solution should fill your bitmap with a solid background color before you start drawing.
Scott Smith
2010-02-24 05:39:07
yes, I save jpg or bmp file.
masoud ramezani
2010-02-24 05:46:55
And your background is *transparent*??? How can that be? Neither BMP or JPG formats support transparency...
Scott Smith
2010-02-24 07:14:56
@Scott: When you create a `System.Drawing.Bitmap`, by default all its pixels are transparent. When you save a `Bitmap` in a format that does not support transparency, the transparent pixels usually end up black.
Zach Johnson
2010-02-24 20:04:57
@Zach: Yeah, I knew about that. But the OP was saying that the resulting jpg or bmp was transparent: *"when I save this bitmap as jpg file, the background of my picture is not a white surface. the background is transparent"*. That's why I kept trying to clarify...
Scott Smith
2010-02-25 00:30:30
@Scott: Ah, ok...
Zach Johnson
2010-02-25 01:28:46