(Winform)控件中添加GIF图片以及运用双缓冲使其不闪烁以及背景是gif时使控件(如panel)变透明

时间:2021-07-22 04:52:19

Image img = Image.FromFile(@"C:\Users\joeymary\Desktop\3.gif");
pictureBox1.Image =img.Clone() as Image;
//label1.Image = img.Clone() as Image;
label1.Size = img.Size;
img.Dispose();

label跟pictureBox中均可使用。

由于窗体重绘会导致控件闪烁

方法一:

/// <summary>
/// 重写Form的CreateParams属性,对控件做二次缓冲(double buffer)
/// </summary>
protected override CreateParams CreateParams//牛逼啊!居然不闪了~~~
{
get
{
CreateParams cp = base.CreateParams;
cp.ExStyle |= 0x02000000;
return cp;
}
}

第二个方法是设置控件的DoubleBuffered属性,需要注意这一属性是私有的,要设置此属性需要使用反射。

这个方法没试过,毕竟看着晕

public static void SetDoubleBuffered(System.Windows.Forms.Control c)
{
//请参考下面的博客
//http://blogs.msdn.com/oldnewthing/archive/2006/01/03/508694.aspx
if (System.Windows.Forms.SystemInformation.TerminalServerSession)
return; System.Reflection.PropertyInfo aProp =
typeof(System.Windows.Forms.Control).GetProperty(
"DoubleBuffered",
System.Reflection.BindingFlags.NonPublic |
System.Reflection.BindingFlags.Instance); aProp.SetValue(c, true, null);
}

//this.panel1.BackColor = Color.Transparent;//将Panel设为透明
//this.panel1.Parent = this.pictureBox1;//将panel父控件设为背景图片控件
//this.panel1.BringToFront();//将panel放在前面