如何使图片框透明?

时间:2013-11-11 15:39:22

标签: c# winforms picturebox

我在C#.NET中创建一个应用程序。我有8个图片框。我使用了具有透明背景的PNG图像,但在我的形式中,当它出现在另一个图像上方时它是不透明的。

我正在使用Visual Studio 2012.这是我的表单截图:

Screenshot of Form

7 个答案:

答案 0 :(得分:49)

执行此操作的一种方法是将重叠图片框的父级更改为其所研究的PictureBox。由于Visual Studio设计器不允许您将PictureBox添加到PictureBox,因此必须在代码(Form1.cs)和Intializing函数中完成:

public Form1()
{
    InitializeComponent();
    pictureBox7.Controls.Add(pictureBox8);
    pictureBox8.Location = new Point(0, 0);
    pictureBox8.BackColor = Color.Transparent;
}

只需将图片框名称更改为您需要的名称即可。这应该返回:

enter image description here

答案 1 :(得分:5)

GameBoard是DataGridView类型的控件; 图像应为具有透明Alpha通道背景的PNG类型;

        Image test = Properties.Resources.checker_black;
        PictureBox b = new PictureBox();
        b.Parent = GameBoard;
        b.Image = test;
        b.Width = test.Width*2;
        b.Height = test.Height*2;
        b.Location = new Point(0, 90);
        b.BackColor = Color.Transparent;
        b.BringToFront();

enter image description here

答案 2 :(得分:0)

尝试使用ImageList

ImageList imgList = new ImageList;

imgList.TransparentColor = Color.White;

像这样加载图像:

picturebox.Image = imgList.Images[img_index];

答案 3 :(得分:0)

我遇到过类似的问题。 您无法轻松制作透明的图片框,例如本页顶部显示的图片,因为.NET Framework和VS .NET对象是由INHERITANCE创建的! (使用父属性)。

我通过RectangleShape解决了这个问题,并使用下面的代码删除了背景, 如果PictureBoxRectangleShape之间的差异不重要且无关紧要,您可以轻松使用RectangleShape

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 
}

答案 4 :(得分:0)

一个快速解决方案是为image1设置图像属性并将backgroundimage属性设置为imag2,唯一的不便是你在图片框中有两个图像,但你可以将背景属性更改为图块,拉伸等。确保背光是透明的。 希望这有帮助

答案 5 :(得分:0)

只需使用Form Paint方法并在其上绘制每个Picturebox即可,它允许透明:

    private void frmGame_Paint(object sender, PaintEventArgs e)
    {
        DoubleBuffered = true;
        for (int i = 0; i < Controls.Count; i++)
            if (Controls[i].GetType() == typeof(PictureBox))
            {
                var p = Controls[i] as PictureBox;
                p.Visible = false;
                e.Graphics.DrawImage(p.Image, p.Left, p.Top, p.Width, p.Height);
            }
    }

答案 6 :(得分:-2)

您可以将PictureBox BackColor属性设置为Transparent