Printing from WPF too slow
I have a WPF application and I need to print from it. I had previously posted a question about printing without needing to display a confirmation window and got a great answer that I have implemented like the following
var pq = LocalPrintServer.GetDefau开发者_StackOverflow社区ltPrintQueue();
var writer = PrintQueue.CreateXpsDocumentWriter(pq);
var paginator = newPass.docMain.Document.DocumentPaginator;
writer.Write(paginator);
This code works just fine and simple - it basically just picks up the local printer que and sends the XPS document. However, it is relatively slow to print. I've tried to narrow down the possibilities and it looks like the greatest speed difference is between WinXP and Win7 machines. On XP it is slow, but acceptable, at about 2-3 seconds to print while on Win7 it can be upwards of 10 seconds and 15 seconds is not uncommon. Is there a reason why this code would have such a difference in speed? Also, I've noticed that there are a few questions on here about WPF print speeds - is there a reason why WPF printing in general is slow?
Perhaps one or more of these links will be useful in solving this issue:
http://www.bradymoritz.com/wpf-printing-performance-and-pdf
http://blogs.infosupport.com/blogs/willemm/archive/2008/11/03/WPF-Speed-tips.aspx
http://answers.microsoft.com/en-us/windows/forum/windows_7-hardware/printing-is-very-very-slow-in-windows-7/477b5aba-a93d-4c5e-ac1f-329fed6459f3
WPF: Why TOO SLOW to Get PrintDialog's .PrintableAreaWidth and .PrintableAreaHeight?
Most printing speed problems that I have seen were related to the printer driver. The task of the printer driver is to translate graphical instructions (coming from WPF in this case) to instructions that the printer understands, the so-called PDL (typically PCL or PostScript). Often, the PDL supports only a subset of the graphical capabilities and consequently complex instructions result in huge PDL jobs. Transparancy flattening being a notorious one.
I've tried to replicate your issue on my own development machine, a Windows 7 (64-bit) PC. With an almost trivial, 1-page FixedDocument
, printing seems to be almost instantaneous (well under 1 second). The printing happened to a standard corporate network-printer, as well as to a local PDF writer (DoPDF), and both performed well. Something definitely seems to be different on your side, and I think you are prematurely assuming that it's a WPF issue.
In order to narrow down your issue, I would consider / try the following:
- Check which line(s) of code take the longest to run. This can be done in a few seconds by putting in lines such as this:
int point1 = Environment.TickCount;
Subtracting subsequent points from each other will give you the time elapsed between the points in milliseconds. Alternatively look into theStopwatch
class. This information should narrow down where the problem is. - Compare print performance from within WPF with performance from other applications. Can a similarly complex MS Word document print more quickly?
- Check what data goes "over the wire" to the printer. It's possible that the data being sent to the printer is much larger than you are expecting, e.g. if it contains images that are not being scaled down at the source.
In general though, it would definitely help if you provided more information about your exact situation, e.g. are you printing over a network, or to USB printers? How large is the document being printed, etc.
The first thing which comes in my mind is to look if the printer driver under Windows 7 is slower. Then i would blame WPF. Try to use another printer or printer driver.
精彩评论