0

在 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);
}

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

4

2 回答 2

1

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

Bitmap bmp = new Bitmap(Panel1.Width, Panel1.Height, Panel1.CreateGraphics());
于 2013-09-19T01:01:12.320 回答
-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)});
}
于 2013-09-19T09:13:52.577 回答