【问题标题】:Print form in Windows application在 Windows 应用程序中打印表单
【发布时间】:2015-05-16 16:37:14
【问题描述】:

我试图获得一个按钮来打印我当前的表单,并尝试了我可以在此处找到的所有代码,但它一直在打印空白页,我不知道为什么。

我使用的代码如下

Bitmap bitmap;
private void btnPrint_Click(object sender, EventArgs e)
{
//Add a Panel control.
Panel panel = new Panel();
this.Controls.Add(panel);

//Create a Bitmap of size same as that of the Form.
Graphics grp = panel.CreateGraphics();
Size formSize = this.ClientSize;
bitmap = new Bitmap(formSize.Width, formSize.Height, grp);
grp = Graphics.FromImage(bitmap);

//Copy screen area that that the Panel covers.
Point panelLocation = PointToScreen(panel.Location);
grp.CopyFromScreen(panelLocation.X, panelLocation.Y, 0, 0, formSize);

//Show the Print Preview Dialog.
printPreviewDialog1.Document = printDocument1;
printPreviewDialog1.PrintPreviewControl.Zoom = 1;
printPreviewDialog1.ShowDialog();
}

private void PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
//Print the contents.
e.Graphics.DrawImage(bitmap, 0, 0);
}

这从表单 (Form2) 上的按钮 (btnPrint) 以及大量文本框和图形运行)

点击后会弹出打印预览对话框,但页面为空白。如果我按打印,它会打印一个空白页。

知道为什么它不复制表单吗??

【问题讨论】:

  • 您通过弄乱该面板将这段代码黑死了。 printDocument1.PrintPage 事件处理程序的事件处理程序看起来很糟糕。使用调试器,在 PrintPage 事件处理程序上设置断点。预测它不会破裂。在设计器中双击 printDocument1 组件以添加事件处理程序。
  • 或者更好:扔掉它,写一个像样的打印例程,而不是将屏幕分辨率转储到您的打印机..
  • 对不起,我对此很陌生,我只是直接从这里的一篇文章中使用了这段代码,只是看不到它在哪里不起作用
  • 我已经编辑了你的标题。请参阅“Should questions include “tags” in their titles?”,其中的共识是“不,他们不应该”。

标签: c# winforms printing desktop-application


【解决方案1】:

请参考:How to: Print Preview a Form

[System.Runtime.InteropServices.DllImport("gdi32.dll")]
public static extern long BitBlt (IntPtr hdcDest, 
int nXDest, int nYDest, int nWidth, int nHeight, 
IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
private Bitmap memoryImage;
private void CaptureScreen()
{
    Graphics mygraphics = this.CreateGraphics();
    Size s = this.Size;
    memoryImage = new Bitmap(s.Width, s.Height, 
    mygraphics);
    Graphics memoryGraphics = Graphics.FromImage(
    memoryImage);
    IntPtr dc1 = mygraphics.GetHdc();
    IntPtr dc2 = memoryGraphics.GetHdc();
    BitBlt(dc2, 0, 0, this.ClientRectangle.Width,
    this.ClientRectangle.Height, dc1, 0, 0, 
    13369376);
    mygraphics.ReleaseHdc(dc1);
    memoryGraphics.ReleaseHdc(dc2);
}

private void printDocument1_PrintPage(System.Object 
sender, System.Drawing.Printing.PrintPageEventArgs e)
{
    e.Graphics.DrawImage(memoryImage, 0, 0);
}

private void printButton_Click(System.Object sender, 
System.EventArgs e)
{
    CaptureScreen();
    printPreviewDialog1.Document = printDocument1;
    printPreviewDialog1.Show();
}

【讨论】:

  • 我在它运行之前使用了这个代码,但是当打印预览窗口出现时它说“文档不包含任何页面”不知道我哪里出错了
  • @MarkBarr 您只上传了一个文本文件而不是项目。所以我不可能在我的电脑上运行你的应用程序。但是我创建了一个简单的项目,它做同样的事情:dropbox.com/s/j4r8m4weislr4fv/PrintPreviewDemo.zip?dl=0 并投票我的答案是否有帮助。 :D
  • 非常感谢该项目无法完全解决与我所做的不同之处,但它确实将表单放入打印预览中,但是有两个问题。 1. 图像不会缩小表单以适应页面,因此它只显示一半的表单和 2. 一旦您从预览打印它并关闭预览窗口,如果您尝试再次打印它会抛出一个异常“你不能访问已处置的对象”
  • 我通过在按下按钮后重新初始化组件来对第二个问题进行排序
  • 很难猜出你到底出了什么问题。如果你能上传你的整个项目,我和其他人会更容易帮助你。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-08
  • 1970-01-01
  • 2010-11-09
  • 1970-01-01
  • 2011-11-14
相关资源
最近更新 更多