Squirrelkiller
Squirrelkiller

Reputation: 2824

Make Picture boxes transparent, each overlapping the other with a corner?

TL;DR: Look at the picture below

So I'm trying to make a little picture, and I and people around me are kinda out of ideas.

I have a table (the sitting+eating one) in the middle (seen from above), and people sitting around it. Those people are round, as isthe table.

Every person has their own picturebox, I just use one picture, rotate it, and set it as image in the next box.

Thep roblem now is: The PictureBoxes of people on corners overlap the table with empty corner, in the image there is transparency there. It should show the table below it, but instead it shows the background of the Form :(

Edit: All backgrounds are set to transparent, the Form has the marble as background and white ("Window") as background colour.

I put one person in the back and one in the front, so it's easy to see:

Here's what's going on

Edit 2 (same as ocmment):

In the last two days I read this question about 10 times, and not one that described this exact problem has had an actual answer. When trying to push one of those, I was told I should post a new question.

Example: How to make picturebox transparent?

Upvotes: 2

Views: 4477

Answers (1)

Zohar Peled
Zohar Peled

Reputation: 82524

Transparency in winforms is kind of misleading, since it's not really transparency.
Winforms controls mimic transparency by painting the part of their parent control they would hide instead of their own background.
However, they will not paint the other controls that might be partially covered by them.
This is the reason your top most picture boxes hides your big picture box.

You can overcome this by creating a custom control that inherits from PictureBox and override its OnPaintBackground method (taken, with slight adjustments, from this code project article):

protected override void OnPaintBackground(PaintEventArgs e)
{
    base.OnPaintBackground(e);
    Graphics g = e.Graphics;

    if (this.Parent != null)
    {
        var index = Parent.Controls.GetChildIndex(this);
        for (var i = Parent.Controls.Count - 1; i > index; i--)
        {
            var c = Parent.Controls[i];
            if (c.Bounds.IntersectsWith(Bounds) && c.Visible)
            {
                using (var bmp = new Bitmap(c.Width, c.Height, g))
                {
                    c.DrawToBitmap(bmp, c.ClientRectangle);
                    g.TranslateTransform(c.Left - Left, c.Top - Top);
                    g.DrawImageUnscaled(bmp, Point.Empty);
                    g.TranslateTransform(Left - c.Left, Top - c.Top);
                }
            }
        }
    }
}

Microsoft have published a Knowledge base article to solve this problem a long time ago, however it's a bit out-dated and it's code sample is in VB.Net.

Another option is to paint the images yourself, without picture boxes to hold them, by using Graphics.DrawImage method.
The best place to do it is probably in the OnPaint method of the form.

Upvotes: 5

Related Questions