我正在尝试打印一个表单:(它有一个tablelauyout,其中4个colomns位于另一个tablelayoutpanel顶部,顶部有一些标签需要打印)但我只得到一个空白页:
public void PrintPanel()
{
PrintDocument printdoc = new PrintDocument();
PrintDialog myPrintDialog = new PrintDialog();
memoryImage = new Bitmap(this.Width, this.Height);
this.tableLayoutPanel1.DrawToBitmap(memoryImage, this.ClientRectangle);
if (myPrintDialog.ShowDialog() == DialogResult.OK)
{
printdoc.PrintPage += printdoc_PrintPage;
System.Drawing.Printing.PrinterSettings values;
values = myPrintDialog.PrinterSettings;
myPrintDialog.Document = printdoc;
printdoc.PrintController = new StandardPrintController();
printdoc.Print();
}
memoryImage.Dispose();
printdoc.Dispose();
}
void printdoc_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 10, 10);
}
所以我把'memoryImage'保存到了一个文件夹但是这也是空白的,所以我错了什么?
我没有使用.Show()
调用表单,代码打印正确,但我想避免用户看到这个。
答案 0 :(得分:0)
获取PrintDocument控件并将其放入表单并调用printDocument1_PrintPage
事件。
Bitmap img;
private void btnPrintForm_Click(object sender, EventArgs e)
{
CaptureScreen();
printDocument1.Print();
printDocument1.PrintPage += new PrintPageEventHandler(printDocument1_PrintPage);
}
private void CaptureScreen()
{
Graphics g = this.CreateGraphics();
Size s = this.Size;
img = new Bitmap(s.Width, s.Height, g);
Graphics mg = Graphics.FromImage(img);
mg.CopyFromScreen(this.Location.X, this.Location.Y, 0, 0, s);
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(img, 0, 0);
}