C# PicBox 背景透明
2012-08-02 16:25
218 查看
private void picBackground_Paint(object sender, PaintEventArgs e)
{
foreach (Control C in this.Controls)
{
if (C is Label)
{
Label L = (Label)C;
L.Visible = false;
//设置绘制文字的格式
StringFormat strFmt = new System.Drawing.StringFormat();
strFmt.Alignment = StringAlignment.Center; //文本垂直居中
strFmt.LineAlignment = StringAlignment.Center; //文本水平居中
e.Graphics.DrawString(L.Text, this.Font, new SolidBrush(this.ForeColor), new RectangleF(L.Left - picBackground.Left, L.Top - picBackground.Top, L.Width, L.Height), strFmt);
}
else if (C is PictureBox)
{
PictureBox L = (PictureBox)C;
if (!L.Name.Equals("picBackground"))
{
L.Visible = false;
ImageAttributes attrib = new ImageAttributes();
//Bitmap img = new Bitmap(L.Image);
Color color = Color.Transparent;
attrib.SetColorKey(color, color);
e.Graphics.DrawImage(L.Image, new Rectangle(L.Left - picBackground.Left, L.Top - picBackground.Top, L.Width, L.Height), 0, 0, L.Image.Width, L.Image.Height, GraphicsUnit.Pixel, attrib);
}
}
}
}
{
foreach (Control C in this.Controls)
{
if (C is Label)
{
Label L = (Label)C;
L.Visible = false;
//设置绘制文字的格式
StringFormat strFmt = new System.Drawing.StringFormat();
strFmt.Alignment = StringAlignment.Center; //文本垂直居中
strFmt.LineAlignment = StringAlignment.Center; //文本水平居中
e.Graphics.DrawString(L.Text, this.Font, new SolidBrush(this.ForeColor), new RectangleF(L.Left - picBackground.Left, L.Top - picBackground.Top, L.Width, L.Height), strFmt);
}
else if (C is PictureBox)
{
PictureBox L = (PictureBox)C;
if (!L.Name.Equals("picBackground"))
{
L.Visible = false;
ImageAttributes attrib = new ImageAttributes();
//Bitmap img = new Bitmap(L.Image);
Color color = Color.Transparent;
attrib.SetColorKey(color, color);
e.Graphics.DrawImage(L.Image, new Rectangle(L.Left - picBackground.Left, L.Top - picBackground.Top, L.Width, L.Height), 0, 0, L.Image.Width, L.Image.Height, GraphicsUnit.Pixel, attrib);
}
}
}
}
相关文章推荐
- C#用了皮肤后,Panel和GroupBox 容器背景透明
- C# winform怎么让pictruebox在其他控件上面背景透明
- C#中实现真正的透明的PictrueBox
- C# WinForm 自定义控件,DataGridView背景透明,TabControl背景透明
- VS2005 C# 使MenuStrip中的图标背景透明
- C#重写控件使其透明背景
- C#把Lable的背景颜色设为透明
- C# OpenFileDialog和PictrueBox
- C#控件背景透明的几种解决方案
- C# 在带有背景图的窗体上加载透明图标
- c#把白色背景变成透明
- C# Winform拖动pictrueBox里的图片到Word中的思路
- C# 用GDI在图片上写字实现lable透明背景
- C#叠加图片,背景为透明的
- C#中保存GIF文件后透明背景问题的一个解决方法
- C#把Lable的背景颜色设为透明
- c#创建透明背景用户控件
- GTK 背景透明button (GtkImage + EventBox)
- VC的GroupBox的背景透明问题
- C#自定义控件背景透明