您的位置:首页 > 编程语言 > C#

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);
}
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: