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.
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?
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.
Thanks!
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With