.net - WPF 打印速度非常慢

标签 .net wpf performance printing

我最近花了很多时间试图弄清楚为什么我正在开发的应用程序(.Net 4.0,WPF 前端)中打印速度如此之慢,但我完全没有想法(打印 150 页需要 25 分钟以上)。

我尝试了各种打印方法(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();

那么我在这里做错了什么?我是否向打印机发送了错误的数据?难道还有什么我没发现的 secret 吗?

编辑 - 这适用于物理打印机以及文件打印机,即 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);
        }

我不使用你使用的循环,因为我从来不需要它。我只是放手,并在稍后到达时处理任何错误(即在我事先检查了打印机状态之后)。要检查打印机状态,您只需查看您正在使用的打印机队列上的状态属性。

我希望这能以任何方式帮助您。

关于.net - WPF 打印速度非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4735140/

相关文章:

asp.net - 使用自签名证书在不同服务器上的 Web 应用程序和 Web API 之间进行通信

c# - 在不终止应用程序的情况下处理全局异常?

c# - WPF - 折线/路径周围的 HitTest 半径

c# - Bing map 与 C# 和 MVVM 信息框和图钉绑定(bind)

performance - GPU 的最佳性能

MySQL查询以获得特定排名

c# - 如何在PHP中获取C#的System.Text?

c# - 如何判断一个IP地址是否属于一个国家

c# - 带有 SharedSizeGroup 列的网格表现得很奇怪(*不是*无限循环)

android - 为什么android :singleLine ="true" make ListView scrolling very laggy?