0

I need a little help understanding transparency order in windows forms. I created a simple form with nothing on it called test.

Within the construction of the form, I created a panel and a picture like so:

public partial class test : Form
{
    public test()
    {
        InitializeComponent();
        //create a panel
        Panel panel = new Panel();
        panel.Location = new Point(10, 10);
        panel.Size = new Size(100, 100);
        panel.BackColor = Color.FromArgb(255, 0, 0);
        panel.Show();
        //put panel on screen
        this.Controls.Add(panel);

        //create a picture box
        PictureBox picture = new PictureBox();
        picture.ImageLocation = "../myPicture2.png";

        picture.Location = new Point(20, 20);
        picture.Size = new Size(100, 100);
        picture.BackColor = Color.Transparent;
        picture.Show();
        this.Controls.Add(picture);

        picture.BringToFront();


    }
}

At first the image I used was myPicture1.png, an image with a white background giving me this result.

enter image description here

But then I cropped out the white background with gimp to make it a transparent background.

enter image description here

However, now the form's background is showing up instead of panel.

enter image description here

When I'm putting this PictureBox on top of the panel I'm trying to keep the background color of the panel behind the Image.

Like this:

enter image description here

Can someone please explain to me how to acheive the desired result of having the panel background behind the transparent image in the picturebox? All advice is greatly appreciated as always!

Jamin
  • 1,362
  • 8
  • 22
  • 1
    Isn't this problem caused by the picturebox being on the form instead of being inside the panel? – crimson589 Dec 14 '17 at 03:55
  • Oh wow that is what i did wrong. Thanks Crimson, i feel really silly right now :S. If you want to put the answer I can select since you helped me :) – Jamin Dec 14 '17 at 03:56

1 Answers1

1

Isn't this problem caused by the picturebox being on the form instead of being inside the panel?

panel.Controls.Add(picture);
Jamin
  • 1,362
  • 8
  • 22
crimson589
  • 1,238
  • 1
  • 20
  • 36