MainWindow.xaml.csの全コード
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Globalization;
namespace WPF_Printing
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = this;
}
private void Print_Click(object sender, RoutedEventArgs e)
{
System.Windows.Controls.PrintDialog printDialog = new System.Windows.Controls.PrintDialog();
if (printDialog.ShowDialog() == true)
{
DrawingVisual dv = new DrawingVisual();
var dc = dv.RenderOpen();
var 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, 100)));
dc.DrawEllipse(Brushes.Red, (System.Windows.Media.Pen)null, new Point(290, 90), 50, 50);
dc.DrawEllipse(Brushes.Blue, (System.Windows.Media.Pen)null, new Point(280, 85), 14, 18);
dc.DrawEllipse(Brushes.Blue, (System.Windows.Media.Pen)null, new Point(320, 85), 14, 18);
rect = new Rect(new System.Windows.Point(240, 50), new System.Windows.Size(100, 30));
dc.DrawRectangle(System.Windows.Media.Brushes.Black, (System.Windows.Media.Pen)null, rect);
dc.DrawLine(new Pen(Brushes.Black, 2), new Point(230, 140), new Point(350, 200));
dc.Close();
printDialog.PrintVisual(dv, "Print");
RenderTargetBitmap bmp = new RenderTargetBitmap(600, 350, 120, 96, PixelFormats.Pbgra32);
bmp.Render(dv);
Image img = new Image { Width = 100, Height = 100, Source = bmp, Stretch = Stretch.Fill };
Width = 500;
Height = 400;
var r = new Rectangle { Fill = new ImageBrush(bmp) };
r.SetValue(Grid.RowProperty, 1);
r.SetValue(Grid.ZIndexProperty, -1);
grid1.Children.Add(r);
}
}
}
}