2013-01-11 39 views
3

我試圖在C#中打印圖像。它是由Adobe Acrobat從PDF創建的完整8.5x11大小的tiff。當我用C#使用下面的代碼進行打印時,它會在垂直方向上打印正確,但不是水平方向,它會在大約半英寸處推出。我將圖像的原點設置爲0,0。我錯過了什麼嗎?如何在C中打印完整大小的圖像#

private FileInfo _sourceFile; 
    public void Print(FileInfo doc, string printer, int tray) 
    { 
     _sourceFile = doc; 
     PrintDocument pd = new PrintDocument(); 
     pd.PrinterSettings.PrinterName = printer; 
     pd.DocumentName = _sourceFile.FullName; 
     using (Image img = Image.FromFile(_sourceFile.FullName)) { 
      if (img.Width > img.Height) { 
       pd.DefaultPageSettings.Landscape = true; 
      } 
     } 
     pd.PrintPage += PrintPage; 
     foreach (PaperSource ps in pd.PrinterSettings.PaperSources) { 
      if (ps.RawKind == tray) { 
       pd.DefaultPageSettings.PaperSource = ps; 
      } 
     } 
     pd.Print(); 
    } 

    private void PrintPage(object o, PrintPageEventArgs e) 
    { 
     using (System.Drawing.Image img = System.Drawing.Image.FromFile(_sourceFile.FullName)) { 
      Point loc = new Point(0, 0); 
      e.Graphics.DrawImage(img, loc); 
     } 
    } 

回答

1

看看代碼從這個下面一個很好的例子,這個代碼是從這個鏈接下面 Print Image in C#

 private void PrintImage() 
     { 
      PrintDocument pd = new PrintDocument(); 

      pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0); 
      pd.OriginAtMargins = false; 
      pd.DefaultPageSettings.Landscape = true; 

      pd.PrintPage += new PrintPageEventHandler(pd_PrintPage); 


      printPreviewDialog1.Document = pd; 
      printPreviewDialog1.ShowDialog(); 

      //pd.Print(); 
     } 

     void pd_PrintPage(object sender, PrintPageEventArgs e) 
     { 
      double cmToUnits = 100/2.54; 
      e.Graphics.DrawImage(bmIm, 100, 1000, (float)(15 * cmToUnits), (float)(10 * cmToUnits)); 
     } 

     private void button5_Click(object sender, EventArgs e) 
     { 
      PrintImage(); 
     } 
+0

不幸的是,這並沒有做任何改動。與之前的工作方式完全相同... – jle

+0

Jle我會檢查這一點我不知道是否有其他的東西,我沒有在你的代碼中看到..這個代碼應該工作..我會明天再次檢查並在我的結尾再次測試.. – MethodMan

+0

我不知道爲什麼我必須設置它。圖像正好是8.5乘11,打印時看起來完全正確,但移動了大約半英寸。我不應該調整圖像的大小以適應它(使用繪圖的超負荷) – jle

相關問題