我想放入背景图片,在背景图片上方我想叠加一个透明的图片框,然后尝试像这样放置第二个图片框:
pictureBox2.BackColor = Color.Transparent;
但这没有用。基本上,我想这样做:
Windows窗体中的透明性没有实现人们所期望的。具有透明背景实际上意味着控件使用其父级的背景。这意味着您需要使覆盖控件成为原始图片框的子控件:
PictureBox overlay = new PictureBox();
overlay.Dock = DockStyle.Fill;
overlay.BackColor = Color.FromArgb(128, Color.Blue);
pictureBox2.Controls.Add(overlay);
如果希望覆盖图片框包含透明图像,则需要实际更改图像:
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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句