none
InvalidPrinterException RRS feed

  • Question

  • Hi Team,

    I have encountered a strange phenomenon, It sometimes and almost randomly happened that printing document (generated inside the application) fails with this exception:

    System.Drawing.Printing.InvalidPrinterException: Settings to access printer are not valid.
      at System.Drawing.Printing.PrinterSettings.GetHdevmodeInternal(String printer)
      at System.Drawing.Printing.PrinterSettings.GetHdevmodeInternal()
      at System.Drawing.Printing.PrinterSettings.GetHdevmode(PageSettings pageSettings)
      at System.Drawing.Printing.PrintController.OnStartPrint(PrintDocument document, PrintEventArgs e)
      at System.Drawing.Printing.StandardPrintController.OnStartPrint(PrintDocument document, PrintEventArgs e)
      at System.Drawing.Printing.PrintController.Print(PrintDocument document)
      at System.Drawing.Printing.PrintDocument.Print()

    We could reproduce the issue inconsistently when running the application in Windows Terminal Server, where more users were logged in. What are the possibilities for occurrence of the above inconsistent issue? 

    Please provide us suggestions to resolve the issue.

    Regards,

    Ayswarya




    Thursday, January 24, 2019 9:32 AM

All replies

  • Hi Ayswarya,

    Thank you for posting here.

    According to your issue, could you share the related code about the exception?

    We are waiting for your update.

    Best regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 25, 2019 5:49 AM
    Moderator
  • Hi Jack,

    Please find the code snippet which we have used.

    System.Drawing.Printing.PrintDocument printDocument = new System.Drawing.Printing.PrintDocument();

                printDocument.PrintPage += PrintDocument_PrintPage;

               var width = 1100;

                var height =848;

                var paperSize = new System.Drawing.Printing.PaperSize();

                paperSize.Width = (int)width;

                paperSize.Height = (int)height;

                printDocument.DefaultPageSettings.PaperSize = paperSize;

                printDocument.Print();

    private void PrintDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)

            {

                using (System.Drawing.Printing.PrintDocument document = sender as System.Drawing.Printing.PrintDocument)

                {

                    // Page size measurements can be centimeter, millimeter,inch, pixel, point..etc

                    // For example page width and height in millimeters here

                    float pageWidth = 210;

                    float pageHeight = 172;

                    // pixel conversion, It can be done for various PDFGraphicsUnits

                    PdfUnitConvertor unitConvertor = new PdfUnitConvertor();

                    // given page size unit is millimeter, so we are converting the pixels from millimeter here

                    pageWidth = unitConvertor.ConvertToPixels((float)pageWidth, PdfGraphicsUnit.Millimeter);

                    pageHeight = unitConvertor.ConvertToPixels((float)pageHeight, PdfGraphicsUnit.Millimeter);

                    SizeF size = new SizeF(pageWidth, pageHeight);

                    BitmapSource bitmapSource = image;

                    Image image = BitmapFromSource(bitmapSource);

                    e.Graphics.DrawImage(image, new PointF(0, 0));

                    pageindex++;

                    if (pageindex < loadedDocument.Pages.Count)

                        e.HasMorePages = true;

                }

            }

    Kindly check this issue and provide the solution to resolve this issue.

    Regards,
    Priyanga.E








    Tuesday, January 29, 2019 9:48 AM
  • Hey guys,

    I also have this phenomenon and can perhaps contribute something to it...

    We have a Windows Server 2016 in use, to which about 5 users connect simultaneously via RDP. There they run an application I programmed in C#, from which they can also print PDF documents. These PDF documents are created in the application itself by components from Syncfusion and also printed by these components (as in the example of Priyanga). At the end of a day, one user (not always the same one) has to print approximately 100 documents in a row. And when printing these documents, this exception occurs every now and then (so far in 1/3 of the cases).
    Ayswarya already showed the interesting stack trace in his first post. There is the method GetHdevmodeInternal [1], which in turn calls the native method DocumentProperties [2] from winspool.drv. It now seems that DocumentProperties returns zero or a negative value, which is why an InvalidPrinterException is thrown in GetHdevmodeInternal. But I still don't know why this call fails. The printer name at least is definitely correct (I can see it in the exception message).
    After this error has occurred, printing on other printers (two more network printers from different manufacturers and even the built-in PDF printer) is no longer possible. The only possibility to circumvent this issue is that the user logs off and on again. After this step printing works perfectly.

    Now I made an interesting discovery in the last days. When users work normally and print a single document from time to time, there is sometimes a process called "splwow64.exe", which is a child process of the application. Before printing the 100 documents at the end of the day, all users quit the application and restart it again. At this point it sometimes happens that for some users this splwow64 process is not stopped when the application terminates, but continues to exist as a kind of orphan process. When a user then tries to print, this InvalidPrinterException occurs. After I killed this splwow64 process manually in Process Explorer and the user immediately tried to print again without logging off and on again, it worked.

    TL;DR:
    It seems to be related to this splwow64 process whether printing is possible or not. If the process does not exist, printing is possible without any problems. But if it exists, printing will fail.

    I hope this information brings new insights and maybe you guys have an idea how to solve this problem.

    My workaround for now is to catch the InvalidPrinterException, search for the user's splwow64 process in the process list (by name and SessionId), and then try to kill the process.


    Best regards,

    Matthias


    ----

    References:

    [1] https://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Printing/PrinterSettings.cs,1004
    [2] https://referencesource.microsoft.com/#System.Drawing/commonui/System/Drawing/Advanced/Gdiplus.cs,4028
    Friday, February 22, 2019 8:41 AM