How to make picturebox transparent?

后端 未结 6 515
故里飘歌
故里飘歌 2020-11-27 16:03

I am making an application in C# .NET. I have 8 picture boxes in it. I used PNG images with transparent background but in my form it is not transparent when it comes above a

6条回答
  •  失恋的感觉
    2020-11-27 16:22

    I've had a similar problem like this. You can not make Transparent picturebox easily such as picture that shown at top of this page, because .NET Framework and VS .NET objects are created by INHERITANCE! (Use Parent Property).

    I solved this problem by RectangleShape and with the below code I removed background, if difference between PictureBox and RectangleShape is not important and doesn't matter, you can use RectangleShape easily.

    private void CreateBox(int X, int Y, int ObjectType)
    {
        ShapeContainer canvas = new ShapeContainer();
        RectangleShape box = new RectangleShape();
        box.Parent = canvas;
        box.Size = new System.Drawing.Size(100, 90);
        box.Location = new System.Drawing.Point(X, Y);
        box.Name = "Box" + ObjectType.ToString();
        box.BackColor = Color.Transparent;
        box.BorderColor = Color.Transparent;
        box.BackgroundImage = img.Images[ObjectType];// Load from imageBox Or any resource
        box.BackgroundImageLayout = ImageLayout.Stretch;
        box.BorderWidth = 0;
        canvas.Controls.Add(box);   // For feature use 
    }
    

提交回复
热议问题