Hello,

I'm having this problem with the transparency in GDI+.
I have 2 pictureBoxes, one over the other, and one of them has its' alpha color set to magenta(255, 0, 255).
My goal is that when the upper pictureBox has a picture on it with a section of the magenta color, that section on the upper pictureBox would show the pictureBox that's below it, at that section, like drawing with 2 layers.

Now, the way I did it, was define the lower picture's BackColor as BackColor::Control(Default), and the upper's as BackColor::Transparent, and when the picture is put to the upper box, the following would be part of the painting code:

bmp->MakeTransparent(Color::Magenta);bmp->MakeTransparent(Color::Magenta);

What this does is when I put a transparent section on the upper, it would show the color of the panel below the pictureBox below it, which I actually want to show.

My question is why does it not show the pictureBox below, and how can I make it so that it does? Or is it even possible at all?

Thanks much for any comments.

Recommended Answers

All 5 Replies

Add your picture to an ImageList then set the transparent color to the imagelist:

ImageList myImages = new ImageList();
myImageList.TransparentColor = Color.Magenta;
myImageList.Images.Add(myImage);

Then when you need to add it to the picturebox:

pictureBox.Image = myImageList.Images[index_of_image];

Please note that I used C# as the coding format as this is the C# forum, not the C++ forum.

Yea, I'm sorry about the C++ format, but I wasn't really sure what it was on GDI+ for C#, I don't use C# much. I came here because no one knew to answer me on the C++ board, and I was tipped to come here since .NET has more support in the C# scene, so I just hope that it's ok?

As for the actual problem, I don't really know, that doesn't seem to have solved it, it still shows the color of the panel underneath the pictureBox that I want to see, and if I remove the MakeTransparent() function I referred to in my original post, then it'll just show the Magenta color, as if the ImageList helps with nothing.

The only difference it did, to which I am a little confused, is that it shrunk the pictures to 16x16 pixels from their original size, however, the pictureBoxes stayed in their proper size..

Am I doing something wrong here?

I'm sorry for the double post, but I have some new input on this.

I've looked over at MSDN to try and realize what the ImageList actually is and why I did it wrong, and I found the errors and fixed them, but then I also got stumped when I realized that the ImageList can only contain images of up to 256x256 pixels...

What do I do if I want transparent layers on larger images?

Well, no, not really.

But I found a solution to my problem already, it's a little cheaty on my part, but atleast it works.

Instead of trying to display the pictureBox contents on a control below the one I want transparent, I made the contents copy out to the backgroundImage value of the pictureBox I want transparent, as the transparency shows the backColor of the control you're currently using, it shows the backgroundImage I set, which is actually the same as the pictureBox I wanted to show, except that I can still work and edit that one as well.

I just hope this could help any other people struggling with transparency here in the future.

Oh, and by the way, I like your signature there, very nice.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.