简体   繁体   English

如何使图片框透明?

[英]How to make picturebox transparent?

I am making an application in C# .NET.我正在用 C# .NET 制作一个应用程序。 I have 8 picture boxes in it.我有 8 个图片框。 I used PNG images with transparent background but in my form it is not transparent when it comes above another image.我使用了具有透明背景的 PNG 图像,但在我的表单中,当它位于另一个图像之上时它不透明。

I am using Visual Studio 2012. This is a screenshot of my form:我正在使用 Visual Studio 2012。这是我的表单的屏幕截图:

表格截图

One way to do this is by changing the parent of the overlapping picture box to the PictureBox over which it is lapping.一种方法是将重叠图片框的父级更改为它正在重叠的图片框。 Since the Visual Studio designer doesn't allow you to add a PictureBox to a PictureBox, this will have to be done in your code (Form1.cs) and within the Intializing function:由于 Visual Studio 设计器不允许您将图片框添加到图片框,因此必须在您的代码 (Form1.cs) 和 Intializing 函数中完成此操作:

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

Just change the picture box names to what ever you need.只需将图片框名称更改为您需要的名称即可。 This should return:这应该返回:

在此处输入图片说明

GameBoard is control of type DataGridView; GameBoard 是 DataGridView 类型的控件; The image should be type of PNG with transparent alpha channel background;图像应为具有透明 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();

在此处输入图片说明

Try using an ImageList尝试使用ImageList

ImageList imgList = new ImageList;

imgList.TransparentColor = Color.White;

Load the image like this:像这样加载图像:

picturebox.Image = imgList.Images[img_index];

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!您不能像本页顶部显示的图片那样轻松制作透明图片框,因为 .NET Framework 和 VS .NET 对象是由 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.我通过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 
}

One fast solution is set image property for image1 and set backgroundimage property to imag2, the only inconvenience is that you have the two images inside the picture box, but you can change background properties to tile, streched, etc. Make sure that backcolor be transparent.一个快速的解决方案是为 image1 设置 image 属性并将 backgroundimage 属性设置为 imag2,唯一的不便是你在图片框内有两个图像,但你可以将背景属性更改为平铺、拉伸等。确保背景色是透明的. Hope this helps希望这可以帮助

Just use the Form Paint method and draw every Picturebox on it, it allows transparency :只需使用 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);
            }
    }

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM