Panel设置背景图后,再加入20个以上的自定义控件,最后再通过Panel. DrawToBitmap的方法,将Panel的背景图与内容控件等一起生成图片。问题是:
在Panel.DrawToBitmap()耗了3秒钟左右,显得太卡了。尝试过使用Control/UserControl替代Panel,也一样卡。生成Bitmap的代码: string s = @"G:\Windows\Web\Wallpaper\Windows\img0.jpg";
Panel p = new Panel(); p.Size = this.Size;
p.BackgroundImage = Image.FromFile(s); for (int i = 0; i <= 20; i++)
{
ctrlFlow ctrl = new ctrlFlow();
if (i % 2 == 0)
ctrl.Left = 0;
else
ctrl.Left = (ctrl.Width + 10); ctrl.Top = (i - i % 2) / 2 * (ctrl.Height + 10);
p.Controls.Add(ctrl);
} p.Invalidate(); Bitmap img=new Bitmap(p.Width,p.Height); Cursor.Current = Cursors.WaitCursor; //在这里需要3s左右,太卡了
p.DrawToBitmap(img, new Rectangle(new Point(0, 0), p.Size)); p.Dispose();
p = null; Cursor.Current = Cursors.Default ; this.BackgroundImage = img;
自定义控件:ctrlFlow:
public partial class ctrlFlow : UserControl
{
public ctrlFlow()
{
InitializeComponent();
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e); e.Graphics.DrawString(this.Name, this.Font, Brushes.Red , new Point(0, 0));
}
}
在Panel.DrawToBitmap()耗了3秒钟左右,显得太卡了。尝试过使用Control/UserControl替代Panel,也一样卡。生成Bitmap的代码: string s = @"G:\Windows\Web\Wallpaper\Windows\img0.jpg";
Panel p = new Panel(); p.Size = this.Size;
p.BackgroundImage = Image.FromFile(s); for (int i = 0; i <= 20; i++)
{
ctrlFlow ctrl = new ctrlFlow();
if (i % 2 == 0)
ctrl.Left = 0;
else
ctrl.Left = (ctrl.Width + 10); ctrl.Top = (i - i % 2) / 2 * (ctrl.Height + 10);
p.Controls.Add(ctrl);
} p.Invalidate(); Bitmap img=new Bitmap(p.Width,p.Height); Cursor.Current = Cursors.WaitCursor; //在这里需要3s左右,太卡了
p.DrawToBitmap(img, new Rectangle(new Point(0, 0), p.Size)); p.Dispose();
p = null; Cursor.Current = Cursors.Default ; this.BackgroundImage = img;
自定义控件:ctrlFlow:
public partial class ctrlFlow : UserControl
{
public ctrlFlow()
{
InitializeComponent();
} protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e); e.Graphics.DrawString(this.Name, this.Font, Brushes.Red , new Point(0, 0));
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货