[英]Very slow printing from WPF
我最近花了很多時間試圖弄清楚為什么我正在處理的應用程序(.Net 4.0,WPF 前端)中的打印速度如此之慢,而且我完全沒有想法(25 分鍾以上)打印 150 頁)。
我已經嘗試了各種打印方法(PrintDialog、XpsDocumentWriter、VisualsToXpsDocument),既使用直接來自控件的矢量數據,也嘗試先渲染控件 (RenderTargetBitmap) 並僅發送圖像,但每種方法給出的結果大致相同。
有趣的是,當使用 VisualsToXpsDocument 進行批量寫入時,我可以在打印框架處理 21 頁所需的時間內創建 186 頁的內容。 這里真的有些不對勁。
為了確保這不僅僅是應用程序中某些控件的復雜性問題,我創建了一個獨立的演示應用程序,其中僅包含一個數據網格,其中包含 4000 行靜態數據和大約 8 列。 數據網格本身沒有性能問題,只是打印。 這是我一直在使用的最被接受的方法,但結果很差。
this.writer
= PrintQueue.CreateXpsDocumentWriter(this.SelectedPrinter.PrintQueue);
PrintingDocumentPaginator paginator
= new PrintingDocumentPaginator(this.PrintConfiguration,
contentSize, pageSize, contentRect, this.printSource, false);
this.writer.WritingProgressChanged += this.OnPrintingProgressChanged;
this.writer.WritingCompleted += this.OnPrintingCompleted;
this.writer.WritingCancelled += this.OnPrintingCanceled;
this.writer.WriteAsync(paginator,
this.PrintConfiguration.PrintTicket, paginator.PageCount);
或者,如果我使用以下代碼,對 EndBatchWrite() 的調用將很快被命中,其余的打印過程將花費更長的時間。
this.writer
= PrintQueue.CreateXpsDocumentWriter(this.SelectedPrinter.PrintQueue);
PrintingDocumentPaginator paginator
= new PrintingDocumentPaginator(this.PrintConfiguration,
contentSize, pageSize, contentRect,
this.printSource, this.useVectorData);
this.writer.WritingProgressChanged += this.OnPrintingProgressChanged;
this.writer.WritingCompleted += this.OnPrintingCompleted;
this.writer.WritingCancelled += this.OnPrintingCanceled;
VisualsToXpsDocument sdf
= (VisualsToXpsDocument)this.writer.CreateVisualsCollator();
for (int i = 0; i < paginator.PageCount; i++)
{
sdf.WriteAsync(paginator.GetPageVisual(i));
}
sdf.EndBatchWrite();
那么我在這里做錯了什么? 我是否向打印機發送了錯誤的數據? 有什么我沒有看到的秘密嗎?
編輯 - 這適用於物理打印機以及文件打印機,即 XPS 打印機、PDF 等。
干杯,
山姆。
這幾乎就是我所做的,這對我來說真的很快:
LocalPrintServer localPrintServer = new LocalPrintServer();
System.Printing.PrintQueue pq = new System.Printing.PrintQueue(localPrintServer, localPrintServer.DefaultPrintQueue.FullName);
System.Windows.Xps.XpsDocumentWriter docWriter = System.Printing.PrintQueue.CreateXpsDocumentWriter(pq);
PrintCapabilities pc = pq.GetPrintCapabilities();
PageImageableArea pia = pc.PageImageableArea;
if (docWriter != null)
{
DocumentPaginator paginator = ((IDocumentPaginatorSource)copy).DocumentPaginator;
// Change the PageSize and PagePadding for the document to match the CanvasSize for the printer device.
paginator.PageSize = new System.Windows.Size(pia.ExtentWidth, pia.ExtentHeight);
// Send content to the printer.
docWriter.Write(paginator);
}
我不使用你使用的循環,因為我從來不需要它。 我只是放手,並在它們稍后到達時處理任何錯誤(即在我事先檢查了打印機狀態之后)。 要檢查打印機狀態,您只需查看正在使用的打印機隊列上的狀態屬性。
我希望這有任何幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.