2011-10-05 26 views
2

私は静かに画像ファイルを印刷しようとしており、特殊な用紙タイプ(「光沢のあるフォト用紙」)と一定のサイズ(15cmで10cm)で印​​刷する必要があります。通常のWindows上でPrinterDialogの使用中に用紙の種類を設定するにはどうすればよいですか?

私が選択することができます7印刷ダイアログ:

サイズ

品質(例えば - "オート"、 "高"、 "標準"、 "カスタム" )

タイプ( "普通紙"、 "光沢写真用紙"、 "写真用紙プラス光沢"、 "写真用紙プロプラチナ"、 "はがき"、等...)

しかし、C#コードでは、PaperSize(6インチの場合は4インチ、= 15cmの場合は10cm)のみを設定しています。

私の問題は....私は紙種類、およびないのPaperSource( "トレイ1"、 "トレイ2"、など)を設定するためのオプションを得るのですか

です私はすべてのプリンタがそれがサポートする独自の用紙タイプを持っていることを知っているので、おそらく私はそれをすべて繰り返す必要があるかもしれませんが、私はちょうどそれを理解できませんでした。

これは私の現在のコードです:

string strPrinterName = "Canon iP4850"; 

PrintDocument printDoc = new PrintDocument(); 

// We set the paper size 
printDoc.DefaultPageSettings.PaperSize = new PaperSize("PhotoPaper", 400, 600); 

// Inside the event i actually draw the image all over the paper by using e.Graphics.DrawImage(...) 
printDoc.PrintPage += PrintDocPrintPage; 

// Creating the print dialog 
PrintDialog dlgPrint = new PrintDialog 
{ 
    Document = printDoc 
}; 

// We choose the printer 
dlgPrint.PrinterSettings.PrinterName = strPrinterName; 

// just to be sure - give the new size of our paper 
dlgPrint.PrinterSettings.DefaultPageSettings.PaperSize = new PaperSize("PhotoPaper", 400, 600); 

// If the printer is invalid 
if (!dlgPrint.PrinterSettings.IsValid) 
{ 
    throw new Exception(@"Printer is invalid" + Environment.NewLine + strPrinterName); 
} 

// Print without showing the dialog 
printDoc.Print(); 

は、事前にすべてに感謝。

+1

イムは、それが不可能であることを言っていないが、そのはかなりであることを行っていません。物理的には、デバイスのDEVMODE構造体(その構造体はプリンタドライバ固有の拡張子を持つ)を取得して、正しい値を設定してから書き戻すことができます。これを行うPrinterSettingsオブジェクトをいくつか助けてくれます。 – user957902

+0

このようなもの[link](http://nicholas.piasecki.name/blog/2008/11/programmatically-selecting-complex-printer-options-in-c-shar/)?私はしようとします... – itsho

+0

はい、あなたがする必要があることをまさにthatsです。 – user957902

答えて

2

私はそれが不可能だと言っているわけではありませんが、それほど美しくはありません。理論的には、デバイスのDEVMODE構造体(その構造体はプリンタドライバ固有の拡張子を持つ)を取得し、正しい値を設定してから書き戻すことができます。これを行うPrinterSettingsオブジェクトのいくつかのヘルパ関数があります。これを行う例がありますhere

+0

注:リンクは壊れていますが、それでも見ることができます[wayback machine](http://web.archive.org/web/20130826203749/http://nicholas.piasecki.name/blog/2008/11/programmatically -selecting-complex-printer-options-in-c-shar /) – itsho

2

これは実際にDEVMODEなしで行うことができます。 PrintTicket.PageMediaTypeプロパティで用紙の種類を設定します。 exampleについて:

// ---------------------- GetPrintTicketFromPrinter ----------------------- 
    /// <summary> 
    /// Returns a PrintTicket based on the current default printer.</summary> 
    /// <returns> 
    /// A PrintTicket for the current local default printer.</returns> 
    public PrintTicket GetPrintTicketFromPrinter() 
    { 
     PrintQueue printQueue = null; 

     var localPrintServer = new LocalPrintServer(); 

     // Retrieving collection of local printer on user machine 
     PrintQueueCollection localPrinterCollection = localPrintServer.GetPrintQueues(); 

     System.Collections.IEnumerator localPrinterEnumerator = 
      localPrinterCollection.GetEnumerator(); 

     if (localPrinterEnumerator.MoveNext()) 
     { 
      // Get PrintQueue from first available printer 
      printQueue = (PrintQueue)localPrinterEnumerator.Current; 
     } 
     else 
     { 
      // No printer exist, return null PrintTicket 
      return null; 
     } 

     // Get default PrintTicket from printer 
     PrintTicket printTicket = printQueue.DefaultPrintTicket; 

     PrintCapabilities printCapabilites = printQueue.GetPrintCapabilities(); 

     // Modify PrintTicket 
     if (printCapabilites.PageMediaTypeCapability.Contains(PageMediaType.CardStock)) 
     { 
      printTicket.PageMediaType = PageMediaType.CardStock; 
     } 

     return printTicket; 
    } 
+0

ありがとうございました!私はそれが後ろから分かっていればいいと思う:-)。それはそれが.Net 3.0から関連していることを言及する価値があるかもしれません。 – itsho

関連する問題