2017-10-18 179 views
0

我已经使用下面的代码来打印Windows窗体的面板。标签不显示在打印

private void button1_Click(object sender, EventArgs e) 
     { 
      System.Drawing.Printing.PrintDocument doc = new System.Drawing.Printing.PrintDocument(); 
      doc.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(Doc_PrintPage); 
      doc.Print(); 
     } 

private void Doc_PrintPage(object sender, PrintPageEventArgs e) 
     { 
      Panel grd = new Panel(); 
      Bitmap bmp = new Bitmap(panel2.Width, panel2.Height, panel2.CreateGraphics()); 
      panel2.DrawToBitmap(bmp, new Rectangle(0, 0, panel2.Width, panel2.Height)); 
      RectangleF bounds = e.PageSettings.PrintableArea; 
      float factor = ((float)bmp.Height/(float)bmp.Width); 
      e.Graphics.DrawImage(bmp, bounds.Left, bounds.Top, bounds.Width, factor * bounds.Width); 

      bmp.Save("test12.jpg"); 
     } 

现在从上面的代码,当我点击按钮打印功能将被打电话,但它将排除其中的标签。我附上图片供您参考。第一个图像是我的UI设计。 enter image description here,当我使用打印功能时,它将删除标签值,如您在其他图像中看到的那样。 enter image description here我已经使用了rectagleshap控件,它们都是粉红色的,我在上面显示标签。我认为标签可能会发回,但是当我使用前端时,它也不会出现。

+0

MCVE中的M表示*最小*。 –

+0

@HansPassant不明白你的意思。 – Developer

回答

0

在我的情况下,标签显示回矩形,所以我添加了一个标签,并将其设置为带前。谢谢您的帮助。

1

你可以在这里试试这个,我用这个来捕捉整个屏幕,它是一个活跃的窗口,就像screencapture或者截图一样。

private void Doc_PrintPage(object sender, PrintPageEventArgs e) 
{ 
    Bitmap bitmap = new Bitmap(panel2.Width, panel2.Height); 
    Graphics graphics = Graphics.FromImage(bitmap as Image); 
    graphics.InterpolationMode = InterpolationMode.Default; 
    graphics.CopyFromScreen(0, 0, 0, 0, bitmap.Size); 
    bitmap.Save(pathDownload + filename + ".jpeg", ImageFormat.Jpeg);                               
    bmp.Save("test12.jpg"); 
} 
+0

感谢代码Ravi,但它显示了所有的细节。让我解释一下。有2个面板,我只想显示面板2的数据。您已经使用panel2的宽度和高度,但仍然捕获整个表单。 – Developer