覆盖在C#Windows应用程序形式的透明的PictureBox? [英] Override a transparent picturebox in C# windows application form?

查看:858
本文介绍了覆盖在C#Windows应用程序形式的透明的PictureBox?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个背景图像和背景图像上面我想塞给一个透明的图片框,并试图把第二个PictureBox的,像这样:

I want to put in a background image, and above the background image I want to superimpose a transparent picturebox and tried to put the second picturebox like so:

pictureBox2.BackColor = Color.Transparent;



但它没有工作。基本上,我想做到这一点:

But it did not work. Basically, I would like to do this:

推荐答案

透明度的 Windows窗体正如人们所期望未实现。有一个透明背景实际上意味着控制使用其父的背景。这意味着你需要让你的覆盖控制原图片框的子:

Transparency in Windows Forms isn't implemented as one would expect. Having a transparent background actually means that a control uses the background of its parent. This means that you need to make your overlay control a child of the original picture box:

PictureBox overlay = new PictureBox();

overlay.Dock = DockStyle.Fill;
overlay.BackColor = Color.FromArgb(128, Color.Blue);

pictureBox2.Controls.Add(overlay);

如果你想覆盖图片框包含你需要真正改变图像的透明图像:

If you want the overlay picture box to contain a transparent image you need to actually change the image:

PictureBox overlay = new PictureBox();
overlay.Dock = DockStyle.Fill;
overlay.BackColor = Color.Transparent;

Bitmap transparentImage = new Bitmap(overlayImage.Width, overlayImage.Height);
using (Graphics graphics = Graphics.FromImage(transparentImage))
{
    ColorMatrix matrix = new ColorMatrix();
    matrix.Matrix33 = 0.5f;

    ImageAttributes attributes = new ImageAttributes();
    attributes.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

    graphics.DrawImage(overlayImage, new Rectangle(0, 0, transparentImage.Width, transparentImage.Height), 0, 0, overlayImage.Width, overlayImage.Height, GraphicsUnit.Pixel, attributes);
}

overlay.Image = transparentImage;

pictureBox2.Controls.Add(overlay);

这篇关于覆盖在C#Windows应用程序形式的透明的PictureBox?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆