How to display a transparent PNG or ICO file in a PictureBox?

  • Thread starter Danyel Meyer - dialog-it GmbH
  • Start date
D

Danyel Meyer - dialog-it GmbH

Hallo NG!

I have a problem with displaying transparent images in PNG or ICO format in
a PicturBox or other controls that can display images.
The problem is, even if the image file is saved with transparency (PNG file
24bit or ICO file), if the background-color of the control or whatever lies
behind the image is not white, the image is displayed with a white box
surrounding it, which is quite annoying.
I´ve even tried to save a PNG with a transparent background defined for
black backgrounds, put it in a PictureBox on a black form, and still a white
box surrounds it.
Any (hopefully simple) way to get my PNGs or ICOs displayed with
transparency? They will be located in an ImageList or as embedded ressources
that fill an ImageList in Sub New() in a VB.NET project...

Many thanks in advance,

--
Danyel Meyer
-------------------------------------------
dialog-it GmbH
Röllinghäuser Strasse 55a
31061 Alfeld/Leine

Tel +49 (0) 5181 900 814
Fax +49 (0) 5181 900 815
E-Mail (e-mail address removed)
 
E

Elisa

Hi Danyel,

Short answer:

Because Windows CE doesn't support transparency, MS automatically turns
transparent pixels into white pixels when loading the bitmap. Look for a
previous post of mine for the complete story.

Long answer:

Their reasoning must be that by replacing the transparent pixels with
white pixels, the bitmaps would show up correctly in for example a
ListView. BUT: that only works if your ListView's background color is
white, and although you can't change a ListView's background color
inside the Form Designer, you can easily change it programmatically.

Currently, the work-around would be:

Don't save bitmaps with a transparent color, but with a real color (a
color that isn't otherwise used within your image). Then, using a custom
control, draw the image yourself, and specify your "fake" transparent
color using the ImageAttributes.SetColorKey method.

There are two problems with this work-around:

1. You can't re-use the same bitmaps in e.g. a ListView, because your
"fake" transparent color will show up (it won't get replaced with the
ListView's background color). To get around this, you could use white as
your "fake" transparent color (but white is ofcourse a very common color
in most images), or use two sets of bitmaps (eating up valuable resource
for an already "underpowered" device).

2. If you don't have control over the bitmaps (e.g. you're showing
images directly downloaded from the web), you would have to somehow
replace the transparent pixels with "fake" transparent pixels on-the-fly.

As you can see, there really is no elegant solution at the moment. If
transparency is indeed not supported in the underlying OS, a better
option would have been if MS gave us a method where we could supply the
replacement color to use when loading bitmaps with transparent pixels.
At least this way we could supply e.g. the Parent.BackColor or similar
to "fake" transparency.


Regards,

Elisa

---------
 
D

Danyel Meyer - dialog-it GmbH

Thanks Elisa.

Heavy stuff about which my boss won?t be very happy... and that causes much
work for me...

Thanks MS.

Kind reguards,
--
Danyel Meyer
-------------------------------------------
dialog-it GmbH
Röllinghäuser Strasse 55a
31061 Alfeld/Leine

Tel +49 (0) 5181 900 814
Fax +49 (0) 5181 900 815
E-Mail danyel.meyer <at> dialog-it.de
 
E

Elisa

Hi Danyel,

Well, my project manager sure wasn't happy about this either. Try
convincing someone that anno 2004 you'll need to spend time writing code
to show transparent bitmaps, bitmap buttons, etc.!


Regards,

Elisa
 
D

Danyel Meyer - dialog-it GmbH

Well Elisa,
That´s exactly what will be it. My boss and me too were thinking that
something like that should be no problem, but we came to know that in CF the
problems are always where noone would expect them...

I´ll see how I can get through this...


--
Danyel Meyer
-------------------------------------------
dialog-it GmbH
Röllinghäuser Strasse 55a
31061 Alfeld/Leine

Tel +49 (0) 5181 900 814
Fax +49 (0) 5181 900 815
E-Mail danyel.meyer <at> dialog-it.de
 
G

Guest

I had to load the png into adobe photo shop and then save it as a transparent gig and it seemed to work but the quality is not as good.
 
D

Danyel Meyer - dialog-it GmbH

Hi, whoever...

I´ve tried what you suggested and loaded a GIF with transparent background
into a PictureBox, but, as what Elisa has written, it is displayed with a
white box surrounding it instead of the background being transparent.
How did you get this to work?


--
Danyel Meyer
-------------------------------------------
dialog-it GmbH
Röllinghäuser Strasse 55a
31061 Alfeld/Leine

Tel +49 (0) 5181 900 814
Fax +49 (0) 5181 900 815
E-Mail danyel.meyer <at> dialog-it.de

csharprun said:
I had to load the png into adobe photo shop and then save it as a
transparent gig and it seemed to work but the quality is not as good.
 
E

Elisa

Hi,

If you load a transparent GIF in a picturebox, the transparent pixels
are replaced with white pixels. As long as your parent control (= the
control that contains your picturebox, i.e. a Form or a Panel) has a
white background, you get the illusion that transparency works, but from
the moment you change the background color of the parent control, you'll
notice transparency doesn't work quite as you expected...


Regards,

Elisa
 

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