将 PrintDialog 和 PrinterSettings 从 winforms 迁移到 wpf

Migrating PrintDialog and PrinterSettings from winforms to wpf

我有以下代码,它在 winforms 和 C# 中运行良好:

printDialog = new PrintDialog();
if (DialogResult.OK == printDialog.ShowDialog())
{
  try
  {
    PrintDocument pd = new PrintDocument();
    PrinterSettings ps = new PrinterSettings();
    pd.PrintPage += new PrintPageEventHandler(PrintImage);
    pd.PrinterSettings = printDialog.PrinterSettings;
    pd.Print();
  }
  catch
  {
  
  }
}

现在在wpf中提示行中有错误:

pd.PrinterSettings = printDialog.PrinterSettings;

所以为了测试其余代码是否有效,我对其进行了评论并且它运行良好,但显然它总是在 PC 默认配置的打印机上打印。

我试图在其他线程中调查如何解决这个问题,解决方案应该如下:

printDialog.PrintQueue = new PrintQueue(new PrintServer(), "The exact name of my printer");

但是执行此操作会产生错误:

Severity Code Description Project File Line Status deleted Error CS0012 Type 'PrintQueue' is defined in an assembly that is not referenced. You must add a reference to the assembly 'System.Printing, Version = 4.0.0.0, Culture = neutral,

欢迎大家提出意见或建议。

wpf 的解决方案是添加对 System.Printing.dll 的引用(感谢 @Sinatr),代码如下:

PrintDocument pd = new PrintDocument();
PrinterSettings ps = new PrinterSettings();
pd.PrintPage += new PrintPageEventHandler(PrintImage);

pd.PrinterSettings.PrinterName = printDialog.PrintQueue.Name; 

pd.Print();