[英]PrintDocument.Print results in Win32Exception The operation completed successfully
[英]PrintDocument.Print() throws a Win32Exception
我从以下代码中得到一个奇怪的异常:
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,表明它已找到打印机,但似乎失败了。
尝试将目标打印机设置为默认打印机(如果尚未设置)并查看是否仍然存在
只有在打印多个文档时才会出现此异常。 我的解决方案是补充
printDocument.Dispose();
在printDocument.Print();
。
为了@ Matt的利益。 我无法弄清楚问题到底是什么,很可能与我们网络的配置有关,但这不在我手中。
相反,我使用了另一种方法,我使用了CommonDialogClass.ShowPhotoPrintingWizard(),它是Interop.WIA的一部分,如下所示。
http://msdn.microsoft.com/en-us/library/windows/desktop/ms630492%28v=vs.85%29.aspx
这将过程移交给照片打印向导,此后我没有遇到任何问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.