gpt4 book ai didi

c# - 如何用c#制作两个透明层?

转载 作者:太空宇宙 更新时间:2023-11-03 23:19:04 25 4
gpt4 key购买 nike

有连续的三层,
picturebox1(.jpg) -> label1 -> picturebox2(.png transparent) 我想要的是让label1和picturebox2对pict​​urebox1透明这样可以通过 picturebox2 看到 label1 但它不起作用..

public Form1()
{
InitializeComponent();
label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent;
pictureBox2.Parent = pictureBox1;
pictureBox2.BackColor = Color.Transparent;
picturebox2.BringToFront();
}

所以请帮帮我

最佳答案

如果你需要一个支持透明度的控件,你应该覆盖控件的绘制并按以下顺序绘制控件:

  • 在位图上绘制受您控制(基于 z-index)的同一容器中的所有控件。
  • 然后在您的控件的图形上绘制该位图。
  • 最后绘制控件的内容。
  • 此外,您的控件的BackColor 应该是Color.Transparent

这是创建 TransparentLabelTransparentPictureBox 控件的结果。在下图中,依次是标签、图像、标签、图像和标签,您可以看到图片框和标签已呈现为具有透明背景并符合 z-index:

enter image description here

透明标签

class TransparentLabel : Label
{
public TransparentLabel()
{
this.BackColor = Color.Transparent;
}
protected override void OnPaint(PaintEventArgs e)
{
if (Parent != null && this.BackColor == Color.Transparent)
{
using (var bmp = new Bitmap(Parent.Width, Parent.Height))
{
Parent.Controls.Cast<Control>()
.Where(c => Parent.Controls.GetChildIndex(c) > Parent.Controls.GetChildIndex(this))
.Where(c => c.Bounds.IntersectsWith(this.Bounds))
.OrderByDescending(c => Parent.Controls.GetChildIndex(c))
.ToList()
.ForEach(c => c.DrawToBitmap(bmp, c.Bounds));

e.Graphics.DrawImage(bmp, -Left, -Top);

}
}
base.OnPaint(e);
}
}

透明图片框

class TransparentPictureBox : PictureBox
{
public TransparentPictureBox()
{
this.BackColor = Color.Transparent;
}
protected override void OnPaint(PaintEventArgs e)
{
if (Parent != null && this.BackColor==Color.Transparent)
{
using (var bmp = new Bitmap(Parent.Width, Parent.Height))
{
Parent.Controls.Cast<Control>()
.Where(c => Parent.Controls.GetChildIndex(c) > Parent.Controls.GetChildIndex(this))
.Where(c => c.Bounds.IntersectsWith(this.Bounds))
.OrderByDescending(c => Parent.Controls.GetChildIndex(c))
.ToList()
.ForEach(c => c.DrawToBitmap(bmp, c.Bounds));

e.Graphics.DrawImage(bmp, -Left, -Top);

}
}
base.OnPaint(e);
}
}

关于c# - 如何用c#制作两个透明层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36099017/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com