界面上的内容比较多吧,我也遇到过,但是我界面上内容比较少。但是似乎是有办法明显减少卡顿的办法的,我晚上到我电脑上找找再回复你
public Main()
{
InitializeComponent();
EnableDoubleBuffering(this.panelFile); // panelFile 为有背景图片的控件
}
private void EnableDoubleBuffering(Control control)
{
typeof(Control).InvokeMember("DoubleBuffered",
System.Reflection.BindingFlags.SetProperty |
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic,
null, control, new object[] { true });
}
那是因为picturebox不是容器控件.你把picturebox移开看看,label位置应该不随picturebox改变,它仍属于Form,透明是针对它所在的容器(Form)透明.
panel是容器控件,所以label可以对它透明.
如果label要对picturebox透明,你必须手写代码把label添加到picturebox的子控件集合中
Label label = new Label();
label.Text = "背景透明";
label.Location = new Point(10, 10);
label.BackColor = Color.Transparent;
pictureBox1.Controls.Add(label);
那就不使用WINFORM提供的picturebox控件,而是直接重写OnPaint方法:
public override void OnPaint(PaintEventArgs e)
{
e.Graphics.DrawImage(this.GetBitmap(), this.ClientRectangle);
}
Bitmap GetBitmap(float Value=50)
{
var source = new Bitmap(this.Image);
if (Value < 1 || Value > 100)
return source;
var effect = new Bitmap(this.Image.Width, this.Image.Height);
var _effect = Graphics.FromImage(effect);
float[][] matrixItems ={new float[]{1,0,0,0,0},
new float [] {0,1,0,0,0},
new float []{0,0,1,0,0},
new float []{0,0,0,0,0},
new float[]{0,0,0, Value / 255f,1}};
var imgMatrix = new ColorMatrix(matrixItems);
var imgEffect = new ImageAttributes();
imgEffect.SetColorMatrix(imgMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
_effect.DrawImage(this.Image, new Rectangle(0, 0, this.Image.Width, this.Image.Height), 0, 0, this.Image.Width, this.Image.Height, GraphicsUnit.Pixel, imgEffect);
return effect;
}