WPF 中的 PrintDialog.PrinterSettings 等效项 [英] PrintDialog.PrinterSettings equivalent in WPF

查看:40
本文介绍了WPF 中的 PrintDialog.PrinterSettings 等效项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我正在编写的 C# WPF 应用程序添加打印支持,但我正在为此烦恼.我正在尝试从 WPF 应用程序的窗口打印单个图像.图像是运输标签,打印机是装有 4"x6" 运输标签纸的热敏打印机.打印代码如下:

I'm trying to add printing support to a C# WPF application I'm writing and I'm tearing my hair out over this. I'm trying to print a single image from a window in a WPF application. The image is a shipping label and the printer is a thermal printer loaded with 4"x6" shipping label stock. The code to print is as follows:

PrintDialog pd = new PrintDialog();

if (pd.ShowDialog() == true)
{
    Image tmpImage = new Image();
    tmpImage.Stretch = Stretch.Uniform;
    tmpImage.Width = pd.PrintableAreaWidth;
    tmpImage.Source = this.img_label.Source;
    tmpImage.Measure(new Size(pd.PrintableAreaWidth, pd.PrintableAreaHeight));
    tmpImage.Arrange(new Rect(new Point(0, 0), tmpImage.DesiredSize));

    pd.PrintVisual(tmpImage, "Shipping Label");
}

此代码的作用是显示打印对话框,我可以选择我的打印机,将其配置为使用正确的标签材料,然后打印标签.但是,正如其他帖子所指出的,它不会保存我选择的设置.因此,如果我选择在不关闭应用程序的情况下再次打印相同的图像,它会恢复为默认打印机,即使我选择了正确的打印机,也会将该打印机默认设置为默认设置,其中包括使用错误尺寸的标签纸.因此,每次打印时,我都必须选择打印机并将其配置为使用正确的库存.这在实际使用中是不可接受的.

This code works in that it will display the print dialog, I can choose my printer, configure it to use the correct label stock, and print the label. However, as other posts have indicated, it does not save the settings I have selected. So, if I choose to print the same image again without closing the application in between, it reverts to the default printer and, even when I choose the correct printer, defaults that printer to the default settings, which includes using the wrong size label stock. So every time I print I have to select the printer and configure it to use the correct stock. This is simply not acceptable in real world use.

在网上大量搜索后,我发现了很多关于此的帖子,但所有帖子都谈到保存 PrintDialog.PrinterSettings 对象,然后使用它来初始化下一个 PrintDialog 实例.但是,在 WPF 中,没有 PrintDialog 类的 PrinterSettings 成员.那是一个 Win Forms 对象.为什么 Win Forms 和 WPF PrintDialog 对象不同是我无法理解的,但这可能是一个无法回答的问题.真正的问题是我现在在做什么.如有必要,我可以重新发明整个轮子并拥有自己的打印机选择器和打印机配置页,并使用 PrintDocument 对象打印图像并完全绕过 PrintDialog.除非完全有必要,否则我宁愿不这样做.显示 PrintDialog 很好,这是人们习惯的,并且它已经具有配置内置打印机的所有能力.但是我如何初始化 PrintDialogWPF 选择正确的打印机并使用正确的打印机设置?如果我使用的是 Windows 窗体,这将是内置的.WPF 等效项是什么?

After much searching online I have found numerous posts about this but all of them talk about saving the PrintDialog.PrinterSettings object and then using that to initialize the next PrintDialog instance. However, in WPF, there is no PrinterSettings member of the PrintDialog class. That is a Win Forms object. Why the Win Forms and WPF PrintDialog objects are different is beyond me, but that's likely a question that won't get answered. The real question is what I do now. I can, if necessary, re-invent the entire wheel and have my own printer selector and printer configuration pages and print the image using a PrintDocument object and bypass the PrintDialog entirely. I'd rather not do this unless it is entirely necessary. Displaying the PrintDialog is nice, it's what people are used to, and it already has all the ability to configure the printer built right in. But how can I initialize the PrintDialog in WPF to select the proper printer and use the proper printer settings? If only I were using Windows Forms this would be built in. What's the WPF equivalent?

第二个问题是,如果没有等效的 WPF,推荐的处理方法是什么?我真的不需要让用户能够在我的应用程序中配置打印机.我想让它做的就是记住他们在下次打印时选择的先前设置,就像曾经编写的每个其他 PC 应用程序一样.怎么会这么难?

The secondary question is, if there is no WPF equivalent, what is the recommended way to handle this? I don't really need to give the user the ability to configure the printer within my application. All I want it to do is remember the previous settings they chose the next time they go to print, just like every single other PC application that has ever been written. How can this be so hard?

任何人都可以提供的任何帮助将不胜感激.与此同时,我正在重新发明众所周知的轮子.我希望尽快得到一个更简单的答案.

Any help anyone can provide would be greatly appreciated. In the mean time I'm going down the path of re-inventing the proverbial wheel. I hope to get an easier answer soon.

谢谢!

推荐答案

WPF 有 PrintTicketPrintQueue 类(并且 PrintDialog 具有相应的属性,可以使用您保存的设置进行初始化).

WPF has PrintTicket and PrintQueue classes (and PrintDialog has corresponding properties, which can be initialized with your saved settings).

为简单起见,您可以将第一个视为纸张设置,将第二个视为打印机设置(所选打印机).

For the simplicity, you can consider the first one as the paper settings, and the second one - as the printer settings (selected printer).

这篇关于WPF 中的 PrintDialog.PrinterSettings 等效项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆