Is it possible to have two overlapping PictureBox controls with transparent images? [duplicate]
Asked Answered
R

2

18

Having two overlapping PictureBox controls, I'm trying to make the transparent areas of the picture box let the controls below (in the z-order) being visible.

Even after trying what Microsoft suggests, I cannot get the desired result.

This is what I currently have:

enter image description here

And this is what I want:

enter image description here

So my question is:

Any way to achieve my desired result with two PictureBox controls (or with another way) that overlap each other and let the transparent areas shine through?

Update:

Actually I solved it by using this answer to the the SO question "Transparent images with C# WinForms".

Recalcitrate answered 10/7, 2012 at 11:17 Comment(4)
what is the ext of the images?Isobar
@Isobar The shield is a PNG file with alpha channel. Why should this be important?Recalcitrate
WinForms does not support this by default. You need to do some very interesting drawing hacks to get it to work. WPF, however, does this without a hitch.Phlox
Same approach as #9387767 That shadow is only going to look good on the correct background so correct placement is crucial.Lauritz
E
21

Try this

private void Form1_Load(object sender, EventArgs e)
{
  // Transparent background...  
  pictureBoxOverlay.BackColor = Color.Transparent;

  // Change parent for overlay PictureBox...
  pictureBoxOverlay.Parent    = pictureBoxMain;

 // Change overlay PictureBox position in new parent...
 // pictureBoxOverlay.Location  = new Point(0, 0);
}

Result

enter image description here

Link

Endamage answered 10/7, 2012 at 12:47 Comment(1)
Thanks, this works unless the overlay picture box is clipped by the picture box. I need them to only partially overlapped.Recalcitrate
I
2

As far as I know, the transparency of a control depends on its parent control (As noted in the link you've given), meaning that in order to have the effect you are looking for, you need to have one picture box nested into another picture box which is impossible given that a picture box is not a container.

You can however, use a custom container control instead of a picture box for the parent image. The most basic control would be a panel. Just set the background image of the control and put the second picture box in it.

Another solution, would be to use one single picture box and manage the rendering manually.

This is by far the best solution as the pseudo-simulated transparency of the other method is quiet inefficient.

Indelible answered 10/7, 2012 at 11:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.