How do I create a transparent PictureBox in a Windows Form? I tried setting the BackColor to Transparent, but that wasn't valid and didn't work.
Printable View
How do I create a transparent PictureBox in a Windows Form? I tried setting the BackColor to Transparent, but that wasn't valid and didn't work.
If u want fully-transparent just turn the visibility property 2 false
i suggest you use a panel loaded with a background picture instead - picturebox cant receive some events either (DragDrop )
I'm not sure that it's possible, some controls in .NET do not behave as expected.
You can always try using a Panel instead. Either set its BackgroundImage property, or subscribe to the Paint event and display your image.
[Edit. cjard beat me to it]
So there's no way at all to do this? I changed the image to a Panel and drew it in OnPaint...but the blank spots are still being filled in with some color instead of showing what's behind the image. It would seem like this is common enough that there would be some way to do it.
Did you set the BackColor to Transparent?
Yep...still draws yellow'ish pixels instead of transparent (i.e. whatever color the object behind them is) ones.
Instead of setting the BackColor of the picturebox to Transparent, try changing the bitmap itself.
Put the code in the ImageChanged event of the PictureBox. It will work if you are not changing the picture all the time.Code:Bitmap pic = new Bitmap(pictureBox1.Image);
for (int x=0; x<pic.Width; x++)
for (int y=0; y<pic.Height; y++)
{
Color pixel = pic.GetPixel(x,y);
if (pixel == someColor) //You might need to compare RGB
pic.SetPixel(x,y,Color.Transparent);
}
pictureBox1.Image = pic;
Here is an article on transparency in Windows Forms: http://www.bobpowell.net/transcontrols.htm. I wouldn't recommend the solution he provides but at least it outlines the general limitations.
Thanks!Quote:
Originally Posted by jhammer
I have a map of strings->Images that I load at startup. This map contains all the files that will be in the picturebox. Also, they're all the same shape, so this really makes the most sense as I know exactly what pixels need to be set to Transparent. Works great!
maybe i misunderstood your question.. i thought you were talking about a transparent PictureBox, but i now realise you are talking about loading a picture that contains transparent pixels, into a picturebox and they arent rendered as transparent.. ?
Quote:
Originally Posted by cjard
That may have been the problem! :DQuote:
Originally Posted by LooselyBased
Congratulations must go to jhammer for working out the real problem.