打印 WPF 位图图像

发布于 2024-08-09 15:08:01 字数 89 浏览 8 评论 0原文

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

谢谢!

What's the best way to print a BitmapImage? I come from a background in System.Drawing so I was thinking about converting it to a Bitmap and then printing it, but I'm thinking there's probably a better way.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

黄昏下泛黄的笔记 2024-08-16 15:08:01

根据 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

Building upon Drew's answer, it is best to measure and arrange the container that is handed to the PrintVisual method. This will prevent an image that is larger than a 8.5 x 11 sheet of paper from being cut off. Here is an example of how I printed an image that was partially visible on-screen:

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 in my example could be replaced with any UIElement container such as a stackpanel, canvas, grid, ect. ImageViewer.Source should be set to the BitmapImage that is ready to be printed.

I got the idea from this page:
http://www.switchonthecode.com/tutorials/printing-in-wpf

魔法唧唧 2024-08-16 15:08:01

查看 PrintDialog。您需要做的就是调用 < code>PrintVisual 方法 传入 Image 作为以 BitmapImage 作为源的视觉对象。

您可能想要设置其他打印选项,但是当您探索 PrintDialog 和相关 API 时您会发现这些选项。

Check out the PrintDialog class. All you should need to do is call the PrintVisual method passing in an Image as the visual that has your BitmapImage as a source.

You may want to setup other printing options, but you'll discover those as you explore PrintDialog and related APIs.

暗喜 2024-08-16 15:08:01

不合逻辑的答案有问题,它总是将图像拉伸到页面大小并在尝试此操作时切断一些内容。

因此我写了一个自己的解决方案,只有拉伸(这是正确的词吗?)如果图像太大,可以使用多重副本和页面方向。

这是代码

illogial's answer has the problem, that's always stretching the image to pagesize and cuts off some content at trying this.

Therefore i wrote a own solution, which only stretch(is this the right word?) if the image is to large, can use multiply copies and pageorientations.

Here is the Code

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文