Class MainWindowクラスの全コード
Imports System.Globalization
Class MainWindow
Public Sub New()
InitializeComponent()
DataContext = Me
End Sub
Private Sub Print_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
Dim printDialog As System.Windows.Controls.PrintDialog = New System.Windows.Controls.PrintDialog()
If printDialog.ShowDialog() Then
Dim dv As DrawingVisual = New DrawingVisual()
Dim dc = dv.RenderOpen()
Dim rect = New Rect(New System.Windows.Point(20, 20), New System.Windows.Size(350, 240))
dc.DrawRoundedRectangle(System.Windows.Media.Brushes.Yellow, New Pen(Brushes.Purple, 2), rect, 20, 20)
dc.DrawText(New FormattedText("WPF Printing", CultureInfo.CurrentCulture, FlowDirection,
New Typeface(New System.Windows.Media.FontFamily("Courier New"), FontStyles.Normal, FontWeights.Bold,
FontStretches.Normal), 13, System.Windows.Media.Brushes.Black), New System.Windows.Point(50, 180))
dc.DrawGeometry(Brushes.Green, New Pen(Brushes.Gray, 2), New RectangleGeometry(New Rect(270, 110, 40, 80)))
dc.DrawGeometry(Brushes.Blue, New Pen(Brushes.Gray, 2), New EllipseGeometry(New Rect(160, 150, 60, 100)))
dc.DrawLine(New Pen(Brushes.Black, 2), New Point(230, 140), New Point(350, 200))
dc.Close()
printDialog.PrintVisual(dv, "TEST-Print")
Dim bmp As RenderTargetBitmap = New RenderTargetBitmap(600, 350, 120, 96, PixelFormats.Pbgra32)
bmp.Render(dv)
Dim img As Image = New Image()
img.Width = 100
img.Height = 100
img.Source = bmp
img.Stretch = Stretch.Fill
Dim r = New Rectangle()
r.Fill = New ImageBrush(bmp)
r.SetValue(Grid.RowProperty, 1)
r.SetValue(Grid.ZIndexProperty, -1)
grid1.Children.Add(r)
End If
End Sub
End Class