如何在C#中打印表单?

时间:2014-07-06 18:19:39

标签: c# winforms

我想打印一张表格。我有这个代码,但我无法选择要打印的打印机,并使用默认打印机进行打印。我该如何解决这个问题?

void PrintImage(object o, PrintPageEventArgs e)
{
    int x = SystemInformation.WorkingArea.X;
    int y = SystemInformation.WorkingArea.Y;
    int width = this.Width;
    int height = this.Height;
    Rectangle bounds = new Rectangle(x, y, width, height);
    Bitmap img = new Bitmap(width, height);
    this.DrawToBitmap(img, bounds);
    Point p = new Point(100, 100);          
    e.Graphics.DrawImage(img, p);
}
private void button1_Click(object sender, EventArgs e)
{
    PrintDocument pd = new PrintDocument();
    pd.PrintPage += new PrintPageEventHandler(PrintImage);

    pd.Print();        
}

1 个答案:

答案 0 :(得分:0)

答案来自HERE

您必须使用PrintDialog

 PrintDocument pd = new PrintDocument();
 pd.PrintPage += new PrintPageEventHandler(PrintPage);
 PrintDialog pdi = new PrintDialog();
 pdi.Document = pd;
 if (pdi.ShowDialog() == DialogResult.OK)
 {
     pd.Print();
 }
 else
 {
      MessageBox.Show("Print Cancelled");
 }

在64位Windows和某些版本的.NET上,您可能需要设置pdi.UseExDialog = true;用于显示对话框窗口。