Printing UI Elements

In Windows Presentation Foundation, it’s very easy to print UI elements. This is possible due to a method from the PrintDialog class, called PrintVisual. The PrintDialog class implements the visualization of the classic print dialog, and PrintVisual can send to any printer (physical or virtual) any object of type System.Windows.Media.Visual, including most WPF controls that inherit from this class.

Let’s look at an example. The following XAML code defines a simple grid with two rows. The first row contains a text message, and the second one contains a button:

<Grid Name="Grid1">    <Grid.RowDefinitions>        <RowDefinition/>        <RowDefinition Height="30"/>    </Grid.RowDefinitions>    <TextBlock Name="TextBlock1" FontSize="26" ...

Get Hidden WPF: Secrets for Creating Great Applications in Windows Presentation Foundation now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.