如何用c#制作两个透明层?
How to make two transparent layer with c#?
有连续的三层,
picturebox1(.jpg) -> label1 -> picturebox2(.png transparent)
我想要的是让label1和picturebox2对picturebox1透明,这样label1就可以透过picturebox2看到,但是没用..
public Form1()
{
InitializeComponent();
label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent;
pictureBox2.Parent = pictureBox1;
pictureBox2.BackColor = Color.Transparent;
picturebox2.BringToFront();
}
所以请帮助我
如果您需要控件支持透明度,您应该覆盖控件的绘制并按以下顺序绘制控件:
- 在位图上绘制受您控制(基于z-index)的同一容器中的所有控件。
- 然后在您的控件的图形上绘制该位图。
- 最后画出你的控件内容。
- 另外你的控件
BackColor
应该是Color.Transparent
.
这是创建 TransparentLabel
和 TransparentPictureBox
控件的结果。在下图中,依次是标签、图像、标签、图像和标签,正如您所见,图片框和标签已呈现为具有透明背景并符合 z-index:
透明标签
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);
}
}
有连续的三层,picturebox1(.jpg) -> label1 -> picturebox2(.png transparent)
我想要的是让label1和picturebox2对picturebox1透明,这样label1就可以透过picturebox2看到,但是没用..
public Form1()
{
InitializeComponent();
label1.Parent = pictureBox1;
label1.BackColor = Color.Transparent;
pictureBox2.Parent = pictureBox1;
pictureBox2.BackColor = Color.Transparent;
picturebox2.BringToFront();
}
所以请帮助我
如果您需要控件支持透明度,您应该覆盖控件的绘制并按以下顺序绘制控件:
- 在位图上绘制受您控制(基于z-index)的同一容器中的所有控件。
- 然后在您的控件的图形上绘制该位图。
- 最后画出你的控件内容。
- 另外你的控件
BackColor
应该是Color.Transparent
.
这是创建 TransparentLabel
和 TransparentPictureBox
控件的结果。在下图中,依次是标签、图像、标签、图像和标签,正如您所见,图片框和标签已呈现为具有透明背景并符合 z-index:
透明标签
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);
}
}