打印面板的内容

时间:2013-09-19 00:48:52

标签: c# winforms

在C#中,我在表单上有一个面板,我要打印的内容。面板的内容是来自DrawLines方法的行。

目前我无法在打印预览中查看或打印面板上的线条。面板的边框确实出现了。

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);            
    panel1.CreateGraphics().DrawLines(new Pen(Color.Black),
      new Point[]{new Point(10,10),new Point(50,50)});
}

private void PrintPanel(Panel pnl)
{
  PrintDialog myPrintDialog = new PrintDialog();
  PrinterSettings values;
  values = myPrintDialog.PrinterSettings;
  myPrintDialog.Document = printDocument1;
  printDocument1.PrintController = new StandardPrintController();
  printDocument1.PrintPage += 
    new System.Drawing.Printing.PrintPageEventHandler(printDocument1_PrintPage);
  printPreviewDialog1.Document = printDocument1;
  printPreviewDialog1.ShowDialog();
  //printDocument1.Print();
  printDocument1.Dispose();
}
void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
{
  Bitmap bmp = new Bitmap(panel1.Width, panel1.Height);
  panel1.DrawToBitmap(bmp, new Rectangle(0, 0, panel1.Width, panel1.Height));
  e.Graphics.DrawImage(bmp, panel1.Width, panel1.Height);
}

为什么面板中的线条没有显示在打印预览或打印中?

2 个答案:

答案 0 :(得分:1)

我想你错过了这个。需要将图形传递到位图

Bitmap bmp = new Bitmap(Panel1.Width, Panel1.Height, Panel1.CreateGraphics());

答案 1 :(得分:-1)

这是因为您以错误的方式使用CreateGraphics并且您的抽奖意外已清除,请尝试在Paint事件处理程序中为您的panel1绘制内容像这样:

//Paint event handler for your panel1
private void panel1_Paint(object sender, PaintEventArgs e){
  e.Graphics.DrawLines(Pens.Black, new Point[]{new Point(10,10),new Point(50,50)});
}