Image transparency problem

Oct 15, 2009 at 9:26 AM


I'm trying to Load a png image in a PictureBox like this (I removed all try/if tests):

PictureBox pb = new PictureBox()

Bitmap b = new Bitmap("\\Temp\\test.png");
Image image = (Image)b;

Texture texture = new Texture();
using (Graphics g = texture.CreateGraphics(image.Width, image.Height, GlesPixelFormat.RGBA, Color.Fuchsia))
g.DrawImage(image, 0, 0);
pb.Image = texture;

 The problem is that I can't see the transparency of my picture, is this normal? Transparencies part are just white :-\


Oct 15, 2009 at 9:03 PM


yes, this is normal. The problem is, that the Bitmap class does not recognize transparency when you load a file. So when you load a transparent image into a bitmap class, the transparency get's lost.

If you want to load a png file, to a picturebox, you can do the following:

            PictureBox box;
            Texture img = new Texture();
            using (FileStream s = new FileStream("\\Temp\\test.png", FileMode.Open))
            box.Image = img;


Oct 16, 2009 at 7:36 AM
Edited Oct 16, 2009 at 8:39 AM

Cool, it works :D

thx a lot.

  oups, that don't realy work. I first did the test with a local image to test it and it worked but in my application, I get the picture from an url so I do :

String url = "http://.....png";

wreq = (HttpWebRequest)WebRequest.Create(url);
wresp = (HttpWebResponse)wreq.GetResponse();

if ((mystream = wresp.GetResponseStream()) != null)
bmp = new Bitmap(mystream);

// then the stuff I wrote on first post.

and it works but if I do it with a texture and try to make img.LoadFromStream(mystream) on a Texture, that doesn't work (a length unrecognized exception)

Oct 20, 2009 at 6:36 PM

I didn't check it, but I suppose that the stream returned does not allow to read the Length property. In that case you would have to copy the stream completely to a memory stream which you then pass to LoadFromStream().