C# Picturebox transparent background doesn’t seem to work

cpictureboxtransparency

For a project of mine I need images to display with a transparent background. I made some .png images that have a transparent background(to check this I opened them in Photoshop). Now I have a class that extends PictureBox:

class Foo : PictureBox
{
    public Foo(int argument)
        : base()
    {
        Console.WriteLine(argument);//different in the real application of course.
        //MyProject.Properties.Resources.TRANSPARENCYTEST.MakeTransparent(MyProject.Properties.Resources.TRANSPARENCYTEST.GetPixel(1,1)); //<-- also tried this
        this.Image = MyProject.Properties.Resources.TRANSPARENCYTEST;
        ((Bitmap)this.Image).MakeTransparent(((Bitmap)this.Image).GetPixel(1, 1));
        this.SizeMode = PictureBoxSizeMode.StretchImage;
        this.BackColor = System.Drawing.Color.Transparent;
    }
}

this however just displays the picturebox with a white background, I just can't seem to make it work with a transparent background.

Best Answer

If you want to overlay images over images (and not images over form), this would make the trick:

overImage.Parent = backImage;
overImage.BackColor = Color.Transparent;
overImage.Location = thePointRelativeToTheBackImage;

Where overImage and backImage are PictureBox with png (with transparent background).

This is because, as said before, the transparency of an image is rendered using the back color of the Parent container. PictureBoxes haven't a "Parent" property so you have to make it manually (or create a cutom control of course).