<Window x:Class="Printing.PrintVisual"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="PrintVisual" Height="259" Width="282">
<StackPanel>
<Canvas Name="canvas">
<TextBlock Canvas.Top="50" Canvas.Left="20" FontSize="25" FontWeight="Bold">Hello There</TextBlock>
<Path Fill="Yellow" Stroke="Blue" Margin="5" Canvas.Top="10" Canvas.Left="10" >
<Path.Data>
<GeometryGroup>
<RectangleGeometry Rect="0 0 100 100"></RectangleGeometry>
<EllipseGeometry Center="50 50" RadiusX="35" RadiusY="25"></EllipseGeometry>
</GeometryGroup>
</Path.Data>
</Path>
</Canvas>
<Button Grid.Row="3" Click="cmdPrint_Click">Print</Button>
</StackPanel>
</Window>
//File:Window.xaml.vb
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes
Namespace Printing
Public Partial Class PrintVisual
Inherits System.Windows.Window
Public Sub New()
InitializeComponent()
End Sub
Private Sub cmdPrint_Click(sender As Object, e As RoutedEventArgs)
Dim printDialog As New PrintDialog()
If printDialog.ShowDialog() = True Then
Dim zoom As Double = 30
canvas.Background = Brushes.LightSteelBlue
canvas.LayoutTransform = New ScaleTransform(zoom / 100, zoom / 100)
Dim pageSize As New Size(printDialog.PrintableAreaWidth - 20, printDialog.PrintableAreaHeight - 20)
canvas.Measure(pageSize)
canvas.Arrange(New Rect(10, 10, pageSize.Width, pageSize.Height))
printDialog.PrintVisual(canvas, "A Scaled Drawing")
canvas.Background = Nothing
canvas.LayoutTransform = Nothing
End If
End Sub
End Class
End Namespace