2017-10-18 346 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