打印WPF BitmapImage

Jam*_*nes 6 .net printing wpf imaging

打印BitmapImage的最佳方法是什么?我来自System.Drawing的背景,所以我正在考虑将其转换为Bitmap然后打印它,但我认为可能有更好的方法.

谢谢!

小智 5

根据Drew的回答,最好测量并安排传递给PrintVisual方法的容器.这样可以防止大于8.5 x 11张纸的图像被切断.以下是我在屏幕上打印部分可见图像的示例:

PrintDialog dlg = new PrintDialog();
bool? result = dlg.ShowDialog();

if (result.HasValue && result.Value)
{
    ImageViewer.Measure(new Size(dlg.PrintableAreaWidth, dlg.PrintableAreaHeight));
    ImageViewer.Arrange(new Rect(new Point(0, 0), ImageViewer1.DesiredSize));

    dlg.PrintVisual(ImageViewer, "Print a Large Image");
}
Run Code Online (Sandbox Code Playgroud)

我的示例中的ImageViewer可以替换为任何UIElement容器,例如stackpanel,canvas,grid等.应将ImageViewer.Source设置为可以打印的BitmapImage.

我从这个页面得到了这个想法:http: //www.switchonthecode.com/tutorials/printing-in-wpf