Friday, February 25, 2011

WPF 300DPI High Resoluation Printing



PrintDialog dlg = new PrintDialog();
GeneralTransform transformToRoot = this.TransformToAncestor(win_root);

Rect rect = new Rect(transformToRoot.Transform(new Point(0, 0)), transformToRoot.Transform(new Point(this.ActualWidth, this.ActualHeight)));

double scale = 300 / 96;

RenderTargetBitmap bitmap = new RenderTargetBitmap((int)(scale * (rect.Width + 1)), (int)(scale * (rect.Height + 1)), scale * 96, scale * 96, PixelFormats.Default);
bitmap.Render(g); // g=grid

Image img = new Image();
img.Source = bitmap;
img.Stretch = Stretch.None;

img.Measure(new Size(dlg.PrintableAreaWidth,
dlg.PrintableAreaHeight));
Size sizeImage = img.DesiredSize;
img.Arrange(new Rect(new Point(0, 0), sizeImage));

dlg.PrintVisual(img, "Test300DPI");

For 3D:

Rect = Viewport3DVisual.ViewPort and it comes from visual brush:

<Border>
<Border.Background>
<VisualBrush Stretch="None">
<VisualBrush.Visual>
<Viewport3DVisual x:Name="visual3d" Viewport="0 0 384 384">


No comments: