Charles Petzold on writing books, reading books, and exercising the internal UTM

Recent Entries
< PreviousBrowse the ArchivesNext >
Subscribe to the RSS Feed

High-Resolution Printing of WPF 3D Visuals

December 7, 2007
Roscoe, N.Y.

I received an email from a reader of 3D-Programmierung für Windows (the German translation of 3D Programming for Windows) about printing scenes composed in WPF 3D.

Some background: In the last chapter of my book (entitled "Applications and Curiosa") I discuss two ways of printing WPF 3D scenes. Both involve the handy PrintVisual method of PrintDialog. Because PrintVisual accepts any object created from a class that derives from Visual, you can print elements such as Viewport3D. However, you generally have more control over the size and placement of the printed image if you instead print objects of type Viewport3DVisual, a class that derives directly from Visual.

Regardless which way you do it, you get jaggies. Apparently WPF 3D scenes always print at 96 DPI. I don't know why this is, but I suppose there's a good reason for it.

My reader in Germany wasn't happy about that and wondered if there's a solution.

More background: When writing 3D Programming for Windows I created many of the visuals from XAML files rendered on a RenderTargetBitmap. When you create such a bitmap, you can specify both the pixel size and the bitmap resolution in dots per inch. This technique allows you to create higher resolution images than you get by the customary screen-shot method, which involves capturing the actual screen image in a bitmap at screen resolution.

I wondered if I could apply that technique to printing: Rather than pass the Viewport3DVisual to the PrintVisual method, first create a RenderTargetBitmap at 300 DPI and call the Render method to render the Viewport3DVisual on the bitmap. Then put the bitmap in an Image element, subject it to a layout pass by calling Measure and Arrange, and pass the Image element to the PrintVisual method.

It worked! Here's the enhanced PrintViewport3DVisual project with two menu items for Normal printing and Hi-Res printing for comparison.


Hi Charles,

Thank you very much for this post. I could try it only now because I had to do another work in the meantime. Your example really helps me. What I discovered, however, is the fact that the position of the printed shape is different from its position on the screen. Do you have an idea how to correct this?

Best regards,

Hanno Blackert, Sat, 26 Jan 2008 07:12:48 -0500 (EST)

Charles, good post.

The "jaggies" have been driving me nuts, Ive been working with the printvisual and the output always looks terrible. My current method actually converts the output to an image format prior to printing, but since Im printing a lot of data, the data gets very large very quickly.

I did notice that printing to an XPS doc does maintain all the original vector characteristics (as it should), but strangely when I print to a PDF doc, even vector graphics are rasterized to the pdf... doesnt seem like this should be so, does it?

What I'd really like to figure out (and it's looking impossible so far) is not just how to print at a higher dpi, but also how to print using mostly vector based data... my laser printer uses PCL, and many printers use postscript, why the heck cant wpf produce actual text and vector graphics outputs to these printing destinations? frustrating...

boomhauer, Wed, 20 Aug 2008 22:46:28 -0400 (EDT)

Recent Entries
< PreviousBrowse the ArchivesNext >
Subscribe to the RSS Feed

(c) Copyright Charles Petzold