2011-04-21 93 views
28

我在PictureBox中有圖像,我想打印它。沒有格式化,沒有什麼,只是打印它。打印圖像c#.net

我一直在谷歌搜索,但我什麼都沒有,只有人打印表格或文字或報告。

private string imgSrc; 

    public string ImgSrc 
    { 
     get { return imgSrc; } 
     set { imgSrc = value; } 
    } 

    public Id_Manager() 
    { 
     ImgSrc = "D:\\Foto.jpg"; 

     InitializeComponent(); 
     idPicture.Load(this.ImgSrc); 
    } 

很明顯,圖像會改變,但現在我只是想打印該圖像。爲了以防萬一,我將這個url保存在一個屬性中。任何幫助?

回答

50

下面的代碼使用PrintDocument對象,您可以將圖像放在printdocument上然後打印出來。

using System.Drawing.Printing; 
... 
protected void btnPrint_Click(object sender, EventArgs e) 
{ 
    PrintDocument pd = new PrintDocument(); 
    pd.PrintPage += PrintPage; 
    pd.Print();  
} 

private void PrintPage(object o, PrintPageEventArgs e) 
{ 
    System.Drawing.Image img = System.Drawing.Image.FromFile("D:\\Foto.jpg"); 
    Point loc = new Point(100, 100); 
    e.Graphics.DrawImage(img, loc);  
} 
+0

非常感謝你這是在最後一行非常有益 – 2011-04-25 19:31:27

+0

,它應該是「IMG」,而不是「 ing「:-) – itsho 2012-02-24 12:59:26

+12

不要忘記在printPage(...)上放置」img「,否則,你會被第二次打印時發生IOException異常:-) – itsho 2012-02-24 13:11:37

8

使用的位置,我有做它該FileInfo的擴展方法:

public static void Print(this FileInfo value) 
{ 
    Process p = new Process(); 
    p.StartInfo.FileName = value.FullName; 
    p.StartInfo.Verb = "Print"; 
    p.Start(); 
}