2012-04-02 50 views
24

C#では、以下のコードでPrintDocumentクラスを使用して画像を印刷しようとしています。画像のサイズは、幅1200ピクセル、高さ1800ピクセルです。私は小さなジブラプリンタを使用してこの画像を4 * 6用紙に印刷しようとしています。しかし、プログラムは4 * 6だけが大きなイメージの印刷です。つまり、画像を用紙サイズに合わせていないということです!PrintDocumentによる画像の印刷。画像を用紙サイズに合わせて調整する方法

 PrintDocument pd = new PrintDocument(); 
    pd.PrintPage += (sender, args) => 
    { 
      Image i = Image.FromFile("C://tesimage.PNG"); 
      Point p = new Point(100, 100); 
      args.Graphics.DrawImage(i, 10, 10, i.Width, i.Height); 
    }; 
    pd.Print(); 

私はウィンドウを印刷使用して同じ画像を印刷するとき(右クリックし、[印刷]を選択し、それが正しく用紙サイズや印刷に自動的にスケーリングされる。すべてのものは4 * 6論文に来た意味。)私はどのように行うのですか私のC#プログラムでも同じですか?

+3

回答が好きな人は、それを受け入れてください。それはあなたに答えた人に信用を与え、適切なものを見つけるために答えを探している他の人々を助けます。 –

答えて

31

DrawImageメソッドに渡すパラメータは、画像自体のサイズではなく、用紙上のイメージのサイズにする必要があります。DrawImageコマンドでスケーリングが行われます。おそらく最も簡単な方法は、DrawImageコマンドの以下のオーバーライドを使うことです。

args.Graphics.DrawImage(i, args.MarginBounds); 

注:画像の割合が矩形と同じでない場合、これは、画像をゆがめるであろう。画像のサイズと用紙サイズの単純な数学によって、画像の歪みを生じることなく用紙の境界に収まる新しい長方形を作成することができます。

+2

これは答えでなければなりません!ソリューションをありがとう、期待どおりに動作します。 – Crushermike

20

私は既にBBoyのまともな回答を踏まえていませんが、私はアスペクト比を維持するコードを行っています。私は彼の提案を取ったので、ここで部分的な信用を得るべきです!

PrintDocument pd = new PrintDocument(); 
pd.DefaultPageSettings.PrinterSettings.PrinterName = "Printer Name"; 
pd.DefaultPageSettings.Landscape = true; //or false! 
pd.PrintPage += (sender, args) => 
{ 
    Image i = Image.FromFile(@"C:\...\...\image.jpg"); 
    Rectangle m = args.MarginBounds; 

    if ((double)i.Width/(double)i.Height > (double)m.Width/(double)m.Height) // image is wider 
    { 
     m.Height = (int)((double)i.Height/(double)i.Width * (double)m.Width); 
    } 
    else 
    { 
     m.Width = (int)((double)i.Width/(double)i.Height * (double)m.Height); 
    } 
    args.Graphics.DrawImage(i, m); 
}; 
pd.Print(); 
+1

良い答えですが、私はargs.MarginBoundsをargs.PageBoundsに置き換えました。 –

2

あなたがここに私のコードを使用することができます

//Print Button Event Handeler 
private void btnPrint_Click(object sender, EventArgs e) 
{ 
    PrintDocument pd = new PrintDocument(); 
    pd.PrintPage += PrintPage; 
    //here to select the printer attached to user PC 
    PrintDialog printDialog1 = new PrintDialog(); 
    printDialog1.Document = pd; 
    DialogResult result = printDialog1.ShowDialog(); 
    if (result == DialogResult.OK) 
    { 
     pd.Print();//this will trigger the Print Event handeler PrintPage 
    } 
} 

//The Print Event handeler 
private void PrintPage(object o, PrintPageEventArgs e) 
{ 
    try 
    { 
     if (File.Exists(this.ImagePath)) 
     { 
      //Load the image from the file 
      System.Drawing.Image img = System.Drawing.Image.FromFile(@"C:\myimage.jpg"); 

      //Adjust the size of the image to the page to print the full image without loosing any part of it 
      Rectangle m = e.MarginBounds; 

      if ((double)img.Width/(double)img.Height > (double)m.Width/(double)m.Height) // image is wider 
      { 
       m.Height = (int)((double)img.Height/(double)img.Width * (double)m.Width); 
      } 
      else 
      { 
       m.Width = (int)((double)img.Width/(double)img.Height * (double)m.Height); 
      } 
      e.Graphics.DrawImage(img, m); 
     } 
    } 
    catch (Exception) 
    { 

    } 
} 
+0

'e.Graphics.DrawImage(img、m);'は私の問題を解決しました.. + –

2

回答:BBOYが提供する

public void Print(string FileName) 
{ 
    StringBuilder logMessage = new StringBuilder(); 
    logMessage.AppendLine(string.Format(CultureInfo.InvariantCulture, "-------------------[ START - {0} - {1} -------------------]", MethodBase.GetCurrentMethod(), DateTime.Now.ToShortDateString())); 
    logMessage.AppendLine(string.Format(CultureInfo.InvariantCulture, "Parameter: 1: [Name - {0}, Value - {1}", "None]", Convert.ToString(""))); 

    try 
    { 
     if (string.IsNullOrWhiteSpace(FileName)) return; // Prevents execution of below statements if filename is not selected. 

     PrintDocument pd = new PrintDocument(); 

     //Disable the printing document pop-up dialog shown during printing. 
     PrintController printController = new StandardPrintController(); 
     pd.PrintController = printController; 

     //For testing only: Hardcoded set paper size to particular paper. 
     //pd.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("Custom 6x4", 720, 478); 
     //pd.DefaultPageSettings.PaperSize = new PaperSize("Custom 6x4", 720, 478); 

     pd.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0); 
     pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0); 

     pd.PrintPage += (sndr, args) => 
     { 
      System.Drawing.Image i = System.Drawing.Image.FromFile(FileName); 

      //Adjust the size of the image to the page to print the full image without loosing any part of the image. 
      System.Drawing.Rectangle m = args.MarginBounds; 

      //Logic below maintains Aspect Ratio. 
      if ((double)i.Width/(double)i.Height > (double)m.Width/(double)m.Height) // image is wider 
      { 
       m.Height = (int)((double)i.Height/(double)i.Width * (double)m.Width); 
      } 
      else 
      { 
       m.Width = (int)((double)i.Width/(double)i.Height * (double)m.Height); 
      } 
      //Calculating optimal orientation. 
      pd.DefaultPageSettings.Landscape = m.Width > m.Height; 
      //Putting image in center of page. 
      m.Y = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Height - m.Height)/2); 
      m.X = (int)((((System.Drawing.Printing.PrintDocument)(sndr)).DefaultPageSettings.PaperSize.Width - m.Width)/2); 
      args.Graphics.DrawImage(i, m); 
     }; 
     pd.Print(); 
    } 
    catch (Exception ex) 
    { 
     log.ErrorFormat("Error : {0}\n By : {1}-{2}", ex.ToString(), this.GetType(), MethodBase.GetCurrentMethod().Name); 
    } 
    finally 
    { 
     logMessage.AppendLine(string.Format(CultureInfo.InvariantCulture, "-------------------[ END - {0} - {1} -------------------]", MethodBase.GetCurrentMethod().Name, DateTime.Now.ToShortDateString())); 
     log.Info(logMessage.ToString()); 
    } 
} 
+0

上記のコードは私のwpf kisokアプリケーションでテストしています。 – KhanSahib

3

ソリューションが正常に動作します。しかし私の場合、私は使用しなければならなかった

e.Graphics.DrawImage(memoryImage, e.PageBounds); 

これはフォームだけを印刷します。 MarginBoundsを使用すると、フォームがモニター画面よりも小さくても、画面全体が印刷されます。 PageBoundsはその問題を解決しました。 BBoyに感謝します!

+0

完璧な、私のために働いた! (@BBoyの回答も良いです) – iedmrc99

4

TonyMとBBoyと合意 - これは、オリジナルの4 * 6ラベル印刷の正解です。 (args.PageBounds)。これはEndicia APIサービス出荷ラベルを印刷するのに役立ちました。

private void SubmitResponseToPrinter(ILabelRequestResponse response) 
    { 
     PrintDocument pd = new PrintDocument(); 
     pd.PrintPage += (sender, args) => 
     { 
      Image i = Image.FromFile(response.Labels[0].FullPathFileName.Trim()); 
      args.Graphics.DrawImage(i, args.PageBounds); 
     }; 
     pd.Print(); 
    } 
+0

'MarginBounds'の印刷方法が小さすぎるので、' PageBounds'が私のために働いていました。ありがとう! – Goose

関連する問題