我从以下代码中得到一个奇怪的异常:

 var printDialog = new PrintDialog();
        printDialog.ShowDialog();

        var printDocument = new PrintDocument { DefaultPageSettings = { Landscape = true, PrinterSettings = new PrinterSettings { PrinterName = printDialog.PrintQueue.Name } } };

        var updateResult = new UpdateResult<Image>(UpdateType.Print) { Success = true };
        foreach (string location in fileLocation)
        {
            try
            {
                _printImage = Image.FromFile(location);
                printDocument.PrintPage += PrintRequest;
            }
            catch (Exception exception)
            {
               //various error handling code here
            }
        }
        printDocument.Print();


最后一行抛出详细信息为“句柄无效”的Win32Exception,根据msdn文档,唯一应抛出的异常是未找到打印机。异常似乎是某种驱动程序/非框架异常。

当我选择打印机(Lexmark T640,设置为直接打印到打印机端口)时,代码可以正常打印,但是选择我可以访问的其他两台打印机之一(另一台T640或深色),则该代码失败。
其他两台打印机设置为通过我们的中央打印服务器进行打印,但是我认为这没有什么区别。
谁能给我任何指示?

编辑:刚刚尝试使用printDialog.PrintQueue.Fullname,并且行为没有不同。替换为垃圾打印机的名称将按预期引发InvalidPrinterException,这表明它已找到打印机,但似乎失败了。

最佳答案

尝试将目标打印机设置为默认打印机(如果尚未设置),然后查看它是否仍然发生

关于c# - PrintDocument.Print()引发Win32Exception,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2014005/

10-11 22:04
查看更多