如何让图片框透明?

问题描述 投票:0回答:7

我正在使用 C# .NET 制作应用程序。我里面有8个图片框。我使用了具有透明背景的 PNG 图像,但在我的表单中,当它位于另一个图像上方时,它不是透明的。

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

Screenshot of Form

c# winforms picturebox
7个回答
76
投票

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

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

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

enter image description here


6
投票

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();


1
投票

尝试使用

ImageList

ImageList imgList = new ImageList;

imgList.TransparentColor = Color.White;

像这样加载图像:

picturebox.Image = imgList.Images[img_index];

0
投票

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

我通过

RectangleShape
解决了这个问题,并使用下面的代码删除了背景, 如果
PictureBox
RectangleShape
之间的区别不重要也不重要,您可以轻松使用
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 
}

0
投票

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


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);
            }
    }

-2
投票

您可以将

PictureBox
BackColor
属性设置为
Transparent

© www.soinside.com 2019 - 2024. All rights reserved.