如何在 C# 中設定列印 DPI
DPI(每英寸點數)控制列印解析度——即印表機在每英寸紙張上放置的墨點數量。 較高的 DPI 可呈現更清晰的文字與更平滑的圖像,但會增加處理時間及碳粉用量。 較低的 DPI 可加快列印速度,且適用於內部草稿。 透過程式設計設定 DPI,可確保每項列印工作皆符合應用程式所需之品質標準,無論使用者預設的印表機設定為何。
IronPrint 在 PrintSettings 類別上公開了一個 Dpi 屬性。 我們將其設定為整數值,將設定傳遞給 Printer.Print(),文件便會以指定解析度列印出來。 預設解析度為 300 DPI,符合商業印刷的標準。
快速入門:設定列印 DPI
- 透過 NuGet 安裝 IronPrint:
Install-Package IronPrint - 將
using IronPrint;加入檔案中 - 建立一個
PrintSettings物件 - 將
Dpi設定為所需解析度(例如 300、600、1200) - 將設定傳遞至
Printer.Print()或Printer.ShowPrintDialog()
最小工作流程(5 個步驟)
- 安裝 IronPrint C# 程式庫
- 建立一個 `PrintSettings` 物件
- 將 `Dpi` 設定為所需解析度
- 將設定傳遞給 `Printer.Print()`
- 執行專案以設定的 DPI 進行列印
如何在 C# 中設定列印 DPI?
PrintSettings 上的 Dpi 屬性接受任何正整數。 預設值為 300,這是商務文件的標準解析度。 實際用於列印的 DPI 可能會受到實體印表機能力的限制——若在最高支援 600 DPI 的印表機上設定 1200 DPI,印表機將使用其最高支援的解析度。
:path=/static-assets/print/content-code-examples/how-to/set-the-dpi/set-the-dpi-office-and-high-res-dpi.cs
using IronPrint;
// Standard office quality (300 DPI)
var officeSettings = new PrintSettings
{
Dpi = 300
};
Printer.Print("invoice.pdf", officeSettings);
// High-resolution output (600 DPI)
var highResSettings = new PrintSettings
{
Dpi = 600
};
Printer.Print("photo-proof.png", highResSettings);
Imports IronPrint
' Standard office quality (300 DPI)
Dim officeSettings As New PrintSettings With {
.Dpi = 300
}
Printer.Print("invoice.pdf", officeSettings)
' High-resolution output (600 DPI)
Dim highResSettings As New PrintSettings With {
.Dpi = 600
}
Printer.Print("photo-proof.png", highResSettings)
在原生 .NET 環境中,要控制列印解析度,必須建立 PrintDocument 物件、存取 DefaultPageSettings.PrinterResolution、處理 PrintPage 事件,並使用 Graphics.DrawImage() 手動渲染內容。 此處包含 15 至 25 行範例程式碼。 IronPrint 將此簡化為設定物件上的單一整數屬性。
不同列印工作應使用哪種 DPI?
選擇合適的 DPI 取決於內容類型與用途。 較高的解析度未必總是更好——對於文字密集的文件,它會增加緩衝區大小和列印時間,卻沒有明顯的效益。
| DPI | 最適合 | 筆記 |
|---|---|---|
| 72–150 | 內部草稿、校樣、測試列印 | 輸出速度快,碳粉用量低 |
| 300 | 商業文件、發票、報告 | IronPrint 預設; 標準商業品質 |
| 600 | 行銷資料、圖形、圖表 | 圖像明顯更清晰,細線更精細 |
| 1200+ | 攝影、檔案保存、美術 | 需搭配相容的印表機; 大型暫存檔 |
對於大多數應用場景而言,300 DPI 能在畫質與效能之間取得最佳平衡。 我們建議先採用預設設定,僅在輸出效果需要明顯提升圖像清晰度或細部細節時才進行調整。
如何將 DPI 與其他列印設定結合使用?
DPI 會與其他 PrintSettings 屬性協同運作,以定義完整的列印工作。 我們可以在同一個物件中設定紙張尺寸、方向、邊距、份數以及灰階模式。
:path=/static-assets/print/content-code-examples/how-to/set-the-dpi/set-the-dpi-combine-dpi-with-settings.cs
using IronPrint;
var settings = new PrintSettings
{
Dpi = 600,
PaperSize = PaperSize.A4,
PaperOrientation = PaperOrientation.Landscape,
PaperMargins = new Margins(10, 10, 10, 10),
NumberOfCopies = 2,
Grayscale = true
};
Printer.Print("quarterly-dashboard.pdf", settings);
Imports IronPrint
Dim settings As New PrintSettings With {
.Dpi = 600,
.PaperSize = PaperSize.A4,
.PaperOrientation = PaperOrientation.Landscape,
.PaperMargins = New Margins(10, 10, 10, 10),
.NumberOfCopies = 2,
.Grayscale = True
}
Printer.Print("quarterly-dashboard.pdf", settings)
將 Grayscale = true 設定為 600 DPI,可產生清晰的單色輸出效果,非常適合用於圖表和資料表。 PaperMargins 的數值單位為毫米。
如何讓使用者在列印對話方塊中調整 DPI?
當我們將 PrintSettings 傳遞給 Printer.ShowPrintDialog() 時,對話方塊會以我們預設的 DPI 開啟。 使用者可在列印前接受設定或調整解析度。
:path=/static-assets/print/content-code-examples/how-to/set-the-dpi/set-the-dpi-dialog-with-dpi-preset.cs
using IronPrint;
// Pre-select 600 DPI, but let the user override
var settings = new PrintSettings
{
Dpi = 600,
PaperSize = PaperSize.Letter
};
Printer.ShowPrintDialog("design-proof.pdf", settings);
IRON VB CONVERTER ERROR developers@ironsoftware.com
針對非阻塞式使用者介面情境,Printer.ShowPrintDialogAsync() 接受相同的參數,並能保持應用程式的響應性。此對話方塊讓使用者在列印前可核對 DPI 是否符合印表機的支援解析度——這在切換使用 600 DPI 辦公室雷射印表機與 1200 DPI 相片印表機時特別有用。 若需完全自動化的工作流程且無需使用者介入,請改用 Printer.Print() 進行靜默列印。
後續步驟
DPI 是 PrintSettings 物件上的單一整數 — 請將其設定為符合各列印工作之品質要求。 商業文件起始字數為 300 字,若為圖像密集型內容則增加至 600 字或以上。
請參閱各可用屬性的列印設定操作指南、Printer 類別的 API 參考以了解完整的方法介面,以及程式碼範例頁面以取得可直接執行的程式碼片段。 IronPrint 教學指南完整介紹了列印的整個生命週期,而變更紀錄則追蹤了最近的更新。
立即開始 30 天試用,在實際專案中測試 DPI 設定。 準備就緒後,請查看起價 $749 的授權方案。

