Alessandro Del Sole's Blog

/* A programming space about Microsoft® .NET® */
posts - 159, comments - 0, trackbacks - 0

My Links


Your host

This is me! This space is about Microsoft® .NET® and Microsoft® Visual Basic development. Enjoy! :-)

These postings are provided 'AS IS' for entertainment purposes only with absolutely no warranty expressed or implied and confer no rights.

Microsoft MVP

My MVP Profile

I'm a VB!

Watch my interview in Seattle

My new book on VB 2015!

Pre-order VB 2015 Unleashed Pre-order my new book "Visual Basic 2015 Unleashed". Click for more info!

My new book on LightSwitch!

Visual Studio LightSwitch Unleashed My book "Visual Studio LightSwitch Unleashed" is available. Click the cover!

Your visits

Follow me on Twitter!

CodePlex download Download my open-source projects from CodePlex!

Article Categories


Post Categories

.NET Framework


Help Authoring

Microsoft & MSDN

Setup & Deployment

Visual Basic 2005/2008/2010

WPF: printing UI elements with Visual Basic 2008

Printing UI elements in Windows Presentation Foundation is a simple task. This can be accomplished via a method called PrintVisual from the PrintDialog class. This last class exposes, as you can imagine, the classical Print dialog and has a method called PrintVisual which can send to a physical o virtual printer any object of type System.Windows.Media.Visual like e.g. the most of WPF controls which are derived from it.


So let's see a very simple code example. In a WPF application created with Visual Basic 2008, the following XAML defines a simple grid with two rows; the first row contains a text message and the second one contains a button:


    <Grid Name="Grid1">



            <RowDefinition Height="30"/>



        <TextBlock Name="TextBlock1" FontSize="26" FontWeight="Bold" Text="WPF Printing exercise" Grid.Row="0"/>

        <Button Grid.Row="1" Width="100" Height="25" Content="Print me!" Name="Button1" />



Double click the button in the designer to see how a Click event handler is automatically generated by the IDE for our button. We can handle the event as follows:


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click


        Dim pDialog As New PrintDialog


        If pDialog.ShowDialog = True Then


            pDialog.PrintVisual(Me, "Window printing")

        End If

    End Sub


Using the instance method PrintVisual in this way will cause the entire window to be printed (Me). The second argument is a free description. We can change the line of code to print just the TextBlock:


            pDialog.PrintVisual(Me.TextBlock1, "A grid")


Obviously this code works also with styled UI elements, therefore not only with controls at minimal style. As we said before, we can send controls also to virtual printers. The following picture shows the result of printing the Window to an XPS document, by selecting the XPS virtual printer in the Print dialog:




How long could it take if you had to do it with Windows Forms? ;-)



Print | posted on venerdì 2 gennaio 2009 18:38 | Filed Under [ Visual Basic Windows Presentation Foundation ]


No comments posted yet.

Post Comment

Please add 1 and 4 and type the answer here:

Powered by:
Powered By Subtext Powered By ASP.NET