为什么我的PrintDialog总是打开"save as"不直接打印?

Why is my PrintDialog always opening "save as" and not print directly?

所以我有一个 Window 想要打印。为此,我创建了一个打印对话框。看起来像这样:

 PrinterSettings settings = new PrinterSettings();
        string Printer = settings.PrinterName;

        System.Windows.Controls.PrintDialog printDlg = new System.Windows.Controls.PrintDialog();
      

        printDlg.PrintQueue = new PrintQueue(new PrintServer(), Printer);
        printDlg.PrintTicket.CopyCount = 1;
        printDlg.PrintTicket.PageOrientation = PageOrientation.Landscape;
        printDlg.PrintVisual(this, "Window Printing.");

但出于某种原因,它会立即打开一个对话框,用于将该程序另存为 PDF。但是我想直接将它打印到我的打印机上,而不需要那个对话框。那么为什么它不打印到我的打印机?我怎样才能让它发挥作用。

您确定选择了正确的打印机吗?因为如果 PrintDialog 的打印机设置为文件打印机,例如 "Microsoft Print to PDF",它将表现得像这样。 (然后,显然,您必须提供打印机应打印到的文件的名称,因此会出现“保存”对话框。)

检查您安装了哪些打印机(或者更确切地说,PrintQueues 您有哪些打印机可用):

LocalPrintServer myPrintServer = new LocalPrintServer();
foreach (PrintQueue pq in myPrintServer.GetPrintQueues())
{
     Console.WriteLine(pq.Name);
}

您确定要设置其中之一吗?你的系统默认的是哪一个?它可能是文件打印机吗?

尝试在代码中将打印机名称设置为正确的物理打印机,并检查是否会打印任何内容:

string myPrinterName = "My Printer"; // <= put an existing name here

LocalPrintServer myPrintServer = new LocalPrintServer();
PrintDialog printDlg = new PrintDialog();  

PrintQueue printQueue = myPrintServer.GetPrintQueue(myPrinterName);
        
printDlg.PrintQueue = printQueue;
printDlg.PrintTicket.CopyCount = 1;
printDlg.PrintTicket.PageOrientation = PageOrientation.Landscape;
 
printDlg.PrintVisual(this, "Window Printing.");

系统中打印机配置是否正确?