2

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?

Community
  • 1
  • 1
Squirrelkiller
  • 2,575
  • 1
  • 22
  • 41
  • Downvote reason: This question have been asked and answered repeatedly all over the internet and in specifically [stackoverflow](http://stackoverflow.com/search?q=%5Bwinforms%5D+transparent+picturebox). Please refer to the Search and research section in [ask]. – Zohar Peled Apr 19 '16 at 07:01
  • 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. – Squirrelkiller Apr 19 '16 at 07:03
  • [Take a look at this article](http://www.codeproject.com/script/Articles/ArticleVersion.aspx?aid=26878&av=611567) – Zohar Peled Apr 19 '16 at 07:07
  • 2
    It is so commonly asked that Microsoft wrote a [KB article for it](https://support.microsoft.com/en-us/kb/943454) 10 years ago. Best to cut the mouse umbilical cord and write code, use Graphics.DrawImage() instead of controls. Layers of paint. – Hans Passant Apr 19 '16 at 07:09
  • Trying to read that kb article...as a trainee who just learned c#, I'm gonna sit here in an hour and haven't fully understood that code. Probaby not even conveted to c#, without undestanding it. Trying Zohars solution for now. – Squirrelkiller Apr 19 '16 at 07:22

1 Answers1

4

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.

Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • Woah thanks dude. Not only the best working, but also on of the easiest answers. Had that article too before, just used the whole TranspControl class. Didn't work as well as your solution :). – Squirrelkiller Apr 19 '16 at 08:14
  • 1
    [Here](http://stackoverflow.com/a/36102074/3110834) is an implementation of `TransparentLabel` and `TransparentPictureBox` which you may find helpful. – Reza Aghaei Apr 19 '16 at 14:08