[原文链接: http://blog.csdn.net/zgke/article/details/3835933]
因对DataGridView控件的打印需求,借鉴了原文的思想得以实现,在此向原文作者表示感谢!
1.
通过Control.DrawToBitmap()方法对DataGridView的当前显示状态截图,存储于PictureBox中
2.
把PictureBox覆盖到DataGridView上
3.
让DataGridView自动调整大小,再次通过Control.DrawToBitmap()方法获取DataGridView的全部图形
1 /// <summary>
2 /// DataGridView跨越滚动条截图
3 /// </summary>
4 /// <param name="dgv">DataGridView</param>
5 /// <returns>图形</returns>
6 private static Image GetDataGridView(DataGridView dgv)
7 {
8 PictureBox pic = new PictureBox();
9 pic.Size = dgv.Size;
10 pic.Location = dgv.Location;
11 Bitmap bmpPre = new Bitmap(pic.Width, pic.Height);
12 dgv.DrawToBitmap(bmpPre, new Rectangle(0, 0, pic.Width, pic.Height));
13 pic.Image = bmpPre;
14 dgv.Parent.Controls.Add(pic);
15
16 dgv.Visible = false;
17 dgv.AutoSize = true;
18
19 Bitmap bmpNew =new Bitmap(dgv.Width,dgv.Height);
20
21 dgv.DrawToBitmap(bmpNew, new Rectangle(0, 0, dgv.Width, dgv.Height));
22
23 dgv.AutoSize = false;
24 dgv.Visible = true;
25
26 dgv.Parent.Controls.Remove(pic);
27 bmpPre.Dispose();
28 pic.Dispose();
29 return bmpNew;
30 }