Reputation: 655
Is there a way to print a WPF control's content without using PrintDialog? I want to avoid PrintDialog because its PrinterSettings.PrintToFile property is ignored unless the user checks it in the dialog. I need to print to a "FILE:" port without showing a print dialog or asking the user to provide a file name.
I looked into PrintDocument which does have the silent PrintToFile capability, but I couldn't find a way to draw the contents of my WPF control onto the document.
Upvotes: 1
Views: 487
Reputation: 5566
If the file format does not matter you can generate an image from the WPF control which you can then save to a file:
private static RenderTargetBitmap ConvertToBitmap(UIElement uiElement, double resolution)
{
dynamic scale = resolution / 96.0;
uiElement.Measure(new Size(Double.PositiveInfinity, Double.PositiveInfinity));
dynamic sz = uiElement.DesiredSize;
dynamic rect = new Rect(sz);
uiElement.Arrange(rect);
dynamic bmp = new RenderTargetBitmap(Convert.ToInt32(scale * (rect.Width)), Convert.ToInt32(scale * (rect.Height)), scale * 96, scale * 96, PixelFormats.Default);
bmp.Render(uiElement);
return bmp;
}
If however you are after something like a postscript output or text file output then this won't be suitable.
Upvotes: 2