Bitmap transparent color not so transparent it seems

P

Peter Oliphant

Using MakeTransparent one can supposedly turn a color used in a Bitmap to
transparent. But, it looks to me like all it does it set these pixels to the
color BackColor of the Control it's attached to. Thus, when I set White to
the transparent color of a Bitmap stored in the Image of a PictureBox and
placed the PictureBox on a Form which has a BackColor of black, it turned
the white pixels of the bitmap image to black, but these black pixels still
cover anything behind them! At least it seems to cover other PictureBox...

Am I missing something here?
 
J

James

Hi,

I don't know how to fix this problem, but whenever I need transparency
these days I just use .gifs. Besides, it will save you a bit of space
and CPU usage for loading the graphics. I've always used them to keep
things simple.

James
 
T

Tamas Demjen

Peter said:
Using MakeTransparent one can supposedly turn a color used in a Bitmap to
transparent.

Try to use PNG. WinForms supports it and it's truly transparent. In
fact, it supports semi-trasparency (with the alpha channel), which lets
you use really nice looking icons. Also a PNG file is a fraction of a
BMP in size.

Tom
 
P

Peter Oliphant

BTW, I tried using JPG, GIF, and PNG files, and all of them still just
change the color of transparent pixels to the background color of the
control which is it's parent, and do not make them transparent. That is,
they still cover up anything behind them, but appear transparent only if the
background is a solid color and the images are not placed over anything
else, including other such 'sprites' which are suppose to have transparent
parts (which it covers with, for example, black pixels instead of seeing
through them).

This means of course it wil not be transparent at all if one uses a picture
for the background instead of a solid color!

Not that big a deal, but it would be nice to get true transparency. Keep in
mind no matter what the original form (i.e. file type) the image comes from
they all end up as internal Bitmap's in the Image property of a PictureBox,
so transparency must be handled at the Bitmap level (not the file level).

Any clues as to how to get TRUE transparency? : )


Peter Oliphant said:
Thanx to both James and Tomas for your replies!!! : )

[==Peter==]

Tamas Demjen said:
Try to use PNG. WinForms supports it and it's truly transparent. In fact,
it supports semi-trasparency (with the alpha channel), which lets you use
really nice looking icons. Also a PNG file is a fraction of a BMP in
size.

Tom
 
T

Tamas Demjen

Peter said:
BTW, I tried using JPG, GIF, and PNG files, and all of them still just
change the color of transparent pixels to the background color of the
control which is it's parent, and do not make them transparent.

Which component are you using it with? Have you set the control's style
to opaque in the constructor?

SetStyle(ControlStyles::Opaque, true);
// control is drawn opaque and the background is not painted

Also try

SetStyle(ControlStyles::UserPaint, true);
// the control paints itself rather than the operating system doing so
SetStyle(ControlStyles::AllPaintingInWmPaint, true);
// ignores the WM_ERASEBKGND

An example of what you're doing would be nice. When I place a PNG with
an alpha channel on my toolbar, it's not only transparent but fully
translucent, meaning the drop shadow has the color of the background.
The capability is there, it's just that most controls paint themselves,
and they're not designed to be fully transparent.

Tom
 
T

Tamas Demjen

Peter said:
BTW, I tried using JPG, GIF, and PNG files, and all of them still just
change the color of transparent pixels to the background color of the
control which is it's parent, and do not make them transparent.

If it's a PictureBox, may I recommend that you set BackColor to
Transparent? If using the IDE, click on the drop-down arrow, go to the
Web tab, and the first color there is Transparent. To do the same
programmatically:

pictureBox1->BackColor = System::Drawing::Color::Transparent;

Tom
 
P

Peter Oliphant

Thanks, Tomas, for responding! : )

Problem is, a line like:

pictureBox1->BackColor = System::Drawing::Color::Transparent;

just changes the BackColor to the specific color that
System::Drawing::Color::Transparent happens to be. That is, if this color is
Black, then it just changes the BackColor of the picture box to black, and
doesn't make it transparent.

What I've been doing is the following:

m_Bitmap->MakeTransparent( trans_color ) ;

Where m_Bitmap is the Bitmap that is pointed to by Image of the PictureBox.
This method is described in the on-line MSDN as:

"Makes the default transparent color transparent for this Bitmap."

But it doesn't. It just displays all of the pixels of color 'trans_color' as
its container's BackColor. Transparency to me means you should be able to
see through the pixel, not just make it a color that will cause it to blend
in with a SOLID COLOR background!

It seems the documentation on this is wrong. I'd love to be wrong instead!
Any ideas? : )

[==P==]
 

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