打印可滚动的窗体。

可能重复:
如何在C#中截取Winforms控件/表单的屏幕截图?

我有一个带有名单和图片列表的窗体。 列表很长,因此有一个滚动面板。 现在,我想打印此表单,但我不能,因为打印function只打印“可见”部分,因为当您向下滚动时看到不可见部分。 那么,有没有办法立刻打印整个表格?

在Visual Basic PowerPacks工具箱中查找“打印表单”控件

要打印可滚动表单的完整客户区,请尝试以下操作…

1.在“工具箱”中,单击“Visual Basic PowerPacks”选项卡,然后将PrintForm组件拖到窗体上。

PrintForm组件将添加到组件托盘中。

2.在“属性”窗口中,将PrintAction属性设置为PrintToPrinter。

3.在相应的事件处理程序中添加以下代码(例如,在打印按钮的Click事件处理程序中)。

1.PrintForm1.Print(Me,PowerPacks.Printing.PrintForm.PrintOption.Scrollable)

试一试,让我知道它是如何为你工作的。

这不完全是一个完整的答案,但这里有一段代码,它在Form上获取可滚动Panel控件的屏幕截图(位图)。 最大的缺点是截屏时屏幕会闪烁。 我已经在简单的应用程序上测试了它,因此它可能无法在所有情况下运行,但这可能是一个开始。

以下是如何使用它:

public partial class Form1 : Form { public Form1() { InitializeComponent(); // create a scrollable panel1 component } private void button1_Click(object sender, EventArgs e) { TakeScreenshot(panel1, "C:\\mypanel.bmp"); } } 

以下是实用程序:

  public static void TakeScreenshot(Panel panel, string filePath) { if (panel == null) throw new ArgumentNullException("panel"); if (filePath == null) throw new ArgumentNullException("filePath"); // get parent form (may not be a direct parent) Form form = panel.FindForm(); if (form == null) throw new ArgumentException(null, "panel"); // remember form position int w = form.Width; int h = form.Height; int l = form.Left; int t = form.Top; // get panel virtual size Rectangle display = panel.DisplayRectangle; // get panel position relative to parent form Point panelLocation = panel.PointToScreen(panel.Location); Size panelPosition = new Size(panelLocation.X - form.Location.X, panelLocation.Y - form.Location.Y); // resize form and move it outside the screen int neededWidth = panelPosition.Width + display.Width; int neededHeight = panelPosition.Height + display.Height; form.SetBounds(0, -neededHeight, neededWidth, neededHeight, BoundsSpecified.All); // resize panel (useless if panel has a dock) int pw = panel.Width; int ph = panel.Height; panel.SetBounds(0, 0, display.Width, display.Height, BoundsSpecified.Size); // render the panel on a bitmap try { Bitmap bmp = new Bitmap(display.Width, display.Height); panel.DrawToBitmap(bmp, display); bmp.Save(filePath); } finally { // restore panel.SetBounds(0, 0, pw, ph, BoundsSpecified.Size); form.SetBounds(l, t, w, h, BoundsSpecified.All); } }