2013-02-05 80 views
0

我試圖在MdiChild中打印pictureBox的內容。 調試代碼看起來像從不觸發PrintPage事件。 我主要在項目中使用此代碼:printing content of a picturebox在MdiChild中打印pictureBox

有什麼問題?

這裏是我的代碼:

private void stampaToolStripMenuItem_Click(object sender, EventArgs e) 
    { 

     Form2 activeChild = this.ActiveMdiChild as Form2; 
     PictureBox theBox = (PictureBox)activeChild.pictureBox1; 
     dastampare = theBox.Image as Bitmap; 
     printDocument1.OriginAtMargins = true; 
     printDocument1.DocumentName = "Prova"; 
     printDialog1.Document = printDocument1; 
     printDialog1.ShowDialog(); 
     if (printDialog1.ShowDialog() == DialogResult.OK) 
     { 
      printDocument1.Print(); 
     } 
    } 

    private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e) 
    { 
     e.Graphics.DrawImage(dastampare, 0, 0); 
    } 
+1

注意:你調用'printDialog1.ShowDialog()'兩次。如果你的調試器到達'printDocument1.Print();'行而不是你的PrintPage事件,那麼你很可能沒有連接事件處理程序。 – LarsTech

+0

你是完全正確的LarsTech。修正它刪除printDialog1.ShowDialog();我仍然無法觸發PrintPage事件。 :( – Podarce

回答

0

在你的窗體的構造,嘗試連線了事件處理程序:

public Form1() { 
    InitializeComponent(); 
    printDocument1.PrintPage += printDocument1_PrintPage; 
} 
+0

現在完美!非常感謝LarsTech! – Podarce