什么是打印BitmapImage的最佳方法?由于我来自System.Drawing背景,所以我在考虑将其转换为Bitmap然后再进行打印,但我认为可能有更好的方法。
谢谢!
谢谢!
在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");
}
在我的例子中,ImageViewer可以替换为任何UIElement容器,例如stackpanel、canvas、grid等。 ImageViewer.Source应该设置为准备好打印的BitmapImage。
我从这个页面得到了灵感: http://www.switchonthecode.com/tutorials/printing-in-wpf
查看{{link1:PrintDialog
类}}。你只需要调用{{link2:PrintVisual
方法}},并将包含BitmapImage
作为源的Image
作为可视化对象传递进去即可。
你可能想要设置其他打印选项,但是当你探索PrintDialog和相关API时,你会发现它们。
illogial的答案存在问题,它总是将图像拉伸到页面大小并在尝试时剪切一些内容。
因此,我编写了自己的解决方案,仅在图像过大时才进行拉伸(这个词是否正确?),可以使用多个副本和页面方向。
这里是代码。