只导出单个控件
Rectangle rect = new Rectangle(0, 0, panel1.Width, panel1.Height);
using (Bitmap bmp = new Bitmap(rect.Width, rect.Height))
{
this.panel1.DrawToBitmap(bmp, rect);
bmp.Save(@"D:\test.png");
}
导出整个窗体
Bitmap bmp= new Bitmap(this.Width, this.Height);//实例化一个和窗体一样大的bitmap
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(this.Left, this.Top, 0, 0, new Size(this.Width, this.Height));//保存整个窗体为图片
bmp.Save(@"D:\test.png");
导出某个控件及该控件上所有的控件
Bitmap bmp= new Bitmap(panel2.Width, panel2.Height);
Graphics g = Graphics.FromImage(bmp);
g.CopyFromScreen(panel2.PointToScreen(Point.Empty), Point.Empty, panel2.Size);//只保存某个控件
bmp.Save(@"D:\test.png");