views:

166

answers:

1

Hi, I'm getting a strange exception from the following code:

 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();

The final line is throwing a Win32Exception with the detail "The handle is invalid", according to the msdn documentation the only exception that should be thrown is printer not found. The exception seems to be to be some sort of driver/non framework exception.

When I select my printer (Lexmark T640, setup to print directly to the printer port) the code prints fine, but selecting either of the other two printers I have access to (another T640, or a dell colour) the code fails. The other two printers are setup to print through our central print server, but I didn't think this should make any difference. Can anyone give me any pointers?

Edit: Just tried it with printDialog.PrintQueue.Fullname and the behaviour is no different. Substituting in a garbage printer name throws an InvalidPrinterException as expected, suggesting it has found the printer, but seems to fail.

A: 

Try setting the target printer as the default printer (if it isn't already) and see if it still happens

Rob Cowell