A PictureBox Problem

Make sure the image in pictureBox3 is transparent. Set the BackColor to transparent. In code, set the Parent property of the pictureBox3 to be pictureBox2. Adjust the Location coordinates of pictureBox3 since they will be relative to the coordinates of pictureBox2 once you’ve changed the Parent.

    private void Form1_Load(object sender, EventArgs e)
    {
        pictureBox3.Parent = pictureBox2;
        pictureBox3.Location =
            new Point(
                pictureBox3.Location.X
                - pictureBox2.Location.X,
                pictureBox3.Location.Y
                - pictureBox2.Location.Y);

    }

In designer you will not see the transparency, but at runtime you will.

Update

In the image, the left side shows the designer view, the right side is the runtime version.
Left: Designer view, Right: How it looks at runtime

Another update

I really don’t understand how it would be possible that this doesn’t work for you. I suppose there must be something we are doing different. I’ll describe the exact steps to take to create a working sample. If you follow the exact same steps, I wonder if we’ll get the same results or not. Next steps describe what to do and use two images I found on the net.

  • Using Visual Studio 2008, create a New Project using template Windows Forms Application. Make sure the project is targeted at the .NET Framework 3.5.
  • Set the Size of the Form to 457;483.
  • Drag a PictureBox control onto the form. Set its Location to 0;0 and its Size to 449;449.
  • Click the ellipsis besides its Image property, click the Import… button and import the image at http://a.dryicons.com/files/graphics_previews/retro_blue_background.jpg (just type the URL in the File name text box and click Open). Then click OK to use the image.
  • Drag another PictureBox onto the form, set its Location to 0;0 and its Size to 256;256. Also set its BackColor property to Transparent.
  • Using the same method as described above, import image http://www.axdn.com/redist/axiw_i.png which is a transparent image.
  • Now place the following code in the form’s OnLoad event handler:

    private void Form1_Load(object sender, EventArgs e)
    {
        pictureBox2.Parent = pictureBox1;
    }
    

That’s it! If I run this program I get a transparent image on top of another image.

Leave a Comment