2013-04-30 184 views
2

我有「C://」中名稱爲「1.bmp」,「2.bmp」,「3.bmp」等位圖,我試圖打印這些圖像,但打印文檔是空的圖像是在正確的路徑)爲什麼我的圖像不能打印到PrintDocument?

這裏是我的代碼:

private void button3_Click_1(object sender, EventArgs e) 
{ 
    PrintDocument pd = new PrintDocument(); 
    for (int indice = 0; indice < nPaginasPDF + 1; indice++) 
    { 
     pd.PrintPage += new PrintPageEventHandler(Print_Page); 
    } 
    PrintPreviewDialog dlg = new PrintPreviewDialog(); 

    dlg.Document = pd; 
    dlg.ShowDialog(); 
    pd.Print(); 
}  

private void Print_Page(object o, PrintPageEventArgs e) 
{ 
    nPaginasImpressas++; 
    System.Drawing.Image i = System.Drawing.Image.FromFile("C:\\" + nPaginasImpressas + ".bmp"); 
    Point p = new Point(891, 1350); 
    e.Graphics.DrawImage(i, p); 
} 
+0

nPaginasImpressas.ToString()何時連接正確? – Edper 2013-04-30 14:20:02

+0

也不起作用 – 2013-04-30 14:21:21

+0

我試圖改變路徑var path =「C:\」+ nPaginasImpressas.ToString()+「.bmp」; 但我得到編譯錯誤 – 2013-04-30 14:28:02

回答

2

確定,因此通過頁面的打印利用了PrintPageEventArgs類,並多次不重視該事件的過程。考慮以下代碼:

private void button3_Click_1(object sender, EventArgs e) 
{ 
    PrintDocument pd = new PrintDocument(); 
    pd.PrintPage += new PrintPageEventHandler(Print_Page); 
    PrintPreviewDialog dlg = new PrintPreviewDialog(); 

    dlg.Document = pd; 
    dlg.ShowDialog(); 
    pd.Print(); 
}  

private void Print_Page(object o, PrintPageEventArgs e) 
{ 
    nPaginasImpressas++; 
    System.Drawing.Image i = System.Drawing.Image.FromFile("C:\\" + nPaginasImpressas + ".bmp"); 
    Point p = new Point(0, 0); 
    e.Graphics.DrawImage(i, p); 

    e.HasMorePages = File.Exists("C:\\" + (nPaginasImpressas + 1) + ".bmp"); 
} 

此代碼應允許您打印多個頁面。但請注意對Point的更改 - 這對我來說是相當可疑的,然後是HasMorePages的槓桿作用。

+0

和程序如何知道有多少頁? – 2013-04-30 14:26:51

+2

@AranhaSilva,只要它在下一個序號中找到位圖,就會繼續打印,請注意這行'e.HasMorePages = File.Exists(「C:\\」+(nPaginasImpressas + 1)+「)。 BMP「);'。 – 2013-04-30 14:27:49

+0

它的效果很好......而且我的代碼也是......問題是Point p = new Point(891,1350); – 2013-04-30 14:34:49