none
Exception while printing raster images via .net RRS feed

  • Question

  • Hello Team,

    We are recently stuck with a problem at creating and printing xps documents on windows 7 machines (The same code works on Windows 8.0/8.1 and 10).

    Here is a rough description of our printing routine:
    1. Create an image control and initialize it with an UriSource.
    2. Create an instance of FixedDocument, which is filled with PageContent, which in turn contains a FixedPage with the image control created in 1.
    3. The created FixedDocument is persisted using XpsDocumentWriter class.
    4. A FixedDocumentSequence is created referencing other xps documents (in our case the one persisted in 3).
    5. An instance of XpsDocumentWriter (created from PrintQueue) is used to print out the FixedDocumentSequence.
       => This is the step where a FileFormatException or ObjectDisposedException is thrown. The stacktrace looks as follows (FileFormatException and ObjectDisposedException):

       bei System.Windows.Media.Imaging.BitmapSource.CriticalCopyPixels(Int32Rect sourceRect, IntPtr buffer, Int32 bufferSize, Int32 stride)
       bei System.Windows.Media.Imaging.BitmapSource.CriticalCopyPixels(Int32Rect sourceRect, Array pixels, Int32 stride, Int32 offset)
       bei Microsoft.Internal.AlphaFlattener.ImageProxy.GetDecodedPixels(Int32Rect bounds)
       bei Microsoft.Internal.AlphaFlattener.ImageProxy.Decode()
       bei Microsoft.Internal.AlphaFlattener.ImageProxy.IsTransparent()
       bei Microsoft.Internal.AlphaFlattener.BrushProxy.IsTransparent()
       bei Microsoft.Internal.AlphaFlattener.GeometryPrimitive.get_IsTransparent()
       bei Microsoft.Internal.AlphaFlattener.Flattener.AddPrimitive(Primitive p)
       bei Microsoft.Internal.AlphaFlattener.Flattener.TreeFlatten(Primitive tree, Geometry clip, Matrix transform, Double opacity, BrushProxy opacityMask)
       bei Microsoft.Internal.AlphaFlattener.Flattener.Convert(Primitive tree, ILegacyDevice dc, Double width, Double height, Double dpix, Double dpiy, Nullable`1 quality)
       bei Microsoft.Internal.AlphaFlattener.MetroDevice0.FlushPage(ILegacyDevice sink, Double width, Double height, Nullable`1 outputQuality)
       bei Microsoft.Internal.AlphaFlattener.MetroToGdiConverter.FlushPage()
       bei System.Windows.Xps.Serialization.NgcSerializationManager.EndPage()
       bei System.Windows.Xps.Serialization.NgcFixedPageSerializer.SerializeObject(Object serializedObject)
       bei System.Windows.Xps.Serialization.NgcDocumentPageSerializer.SerializeObject(Object serializedObject)
       bei System.Windows.Xps.Serialization.NgcDocumentPaginatorSerializer.SerializeObject(Object serializedObject)
       bei System.Windows.Xps.Serialization.NgcSerializationManager.SaveAsXaml(Object serializedObject)
       bei System.Windows.Xps.XpsDocumentWriter.SaveAsXaml(Object serializedObject, Boolean isSync)
       bei System.Windows.Xps.XpsDocumentWriter.Write(DocumentPaginator documentPaginator)
       bei PrintTest.NDPrintJob.PrintViaXPSWriter(DocumentPaginator oDocumentPaginator) in C:\Public\PrintTest\PrintTest\NDPrintJob.cs:Zeile 182.
       bei PrintTest.MainWindow.Print() in C:\Public\PrintTest\PrintTest\MainWindow.xaml.cs:Zeile 120.
       bei PrintTest.MainWindow.Print_Click(Object sender, RoutedEventArgs e) in C:\Public\PrintTest\PrintTest\MainWindow.xaml.cs:Zeile 65.

    We figured out that the FileFormatException is only thrown in the following cases:
    -Windows 7 (32 or 64 bit) with SP1 (at least KB2670838 must be installed) or equivalent to it Windows Server 2008
    -The selected printer is a local one (not routed; e.g. CutePDF or Network printer) and is not the XPS Document Writer printer.
    -Occurs on all file formats (tiff, png, jpeg etc.)
    -In cases of tiff images with an indexed pixel format it helped to change the format to Format32bppArgb.

    The solution (1drv.ms/1QiqEB3) can be used to reproduce the described issue. Here are the steps to do:
    1. Click on Button "Load Image"
    (opt.) Click on "Show Print Dialog" to choose a printer
    2. Click on Button "Create XPS-File"
    3. Click on Button "Print via XpsDocumentWriter"

    Best Regards and Thanks in advance

    Peter

       
    Tuesday, February 16, 2016 9:50 AM

All replies

  • Hi Peter,

    The solution (1drv.ms/1QiqEB3) can be used to reproduce the described issue. Here are the steps to do:
    1. Click on Button "Load Image"
    (opt.) Click on "Show Print Dialog" to choose a printer
    2. Click on Button "Create XPS-File"
    3. Click on Button "Print via XpsDocumentWriter"

    What do you mean by "1drv.ms/1QiqEB3"? I am not completely sure what you are trying to achieve and what the real scenario is, I would suggest you upload your project to OneDrive. We will take a look and test on win7 SP1 to see the result.

    >>We figured out that the FileFormatException is only thrown in the following cases:
    -Windows 7 (32 or 64 bit) with SP1 (at least KB2670838 must be installed) or equivalent to it Windows Server 2008         

    Does this issue related to KB2670838 ? Do you have a  test? How about the result if you uninstall that KB?

    From your side, I would suggest you try to debug your code on win7SPI machine, since it works on Windows 8.0/8.1 and 10, I think the code is Ok, I believe there is something new feature missing in Win7 SP1 or not supported feature.  

    Here I found a thread that talked about Unable to print .xps files with Windows 7 , please take a look. Beside these, I also suggest you repost this issue in win7 Community forum for better support. http://answers.microsoft.com/en-us/windows/forum/windows_7

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Edited by Kristin Xie Wednesday, February 17, 2016 2:23 AM
    Wednesday, February 17, 2016 2:14 AM
  • Hi Kristin,

    thanks for the quick reply.

    -1drv.ms/1QiqEB3 is the hyperlink to my onedrive with the solution archive (link). Since i was not able to post hyperlinks in my question i removed the "http://" to still be able to post it. Feel free to take a look at the solution.

    -Removing KB2670838 helped partially. We were able to print xps documents on other printers without the mentioned exceptions but sometimes the first page was not rendered (turned out empty).

    -As mentioned above the problem only occurs on Windows 7 / Windows Server 2008 machines. Everything above works fine.

    Meanwhile i will repost the question in the win 7 community as you suggested.

    Thanks for the support.

    Best regards

    Peter

    Wednesday, February 17, 2016 7:45 AM
  • Hi Kristin,

    thanks for the quick reply.

    -1drv.ms/1QiqEB3 is the hyperlink to my onedrive with the solution archive (link). Since i was not able to post hyperlinks in my question i removed the "http://" to still be able to post it. Feel free to take a look at the solution.

    -Removing KB2670838 helped partially. We were able to print xps documents on other printers without the mentioned exceptions but sometimes the first page was not rendered (turned out empty).

    -As mentioned above the problem only occurs on Windows 7 / Windows Server 2008 machines. Everything above works fine.

    Meanwhile i will repost the question in the win 7 community as you suggested.

    Thanks for the support.

    Best regards

    Peter

    Hi Peter,

    Thanks for your detailed explanation.

    Now I've download your project and test on win7 Enterprise SP1. Follow your steps.  It works fine on my side

    I also checked my machine, it doesn't intall KB2670838.  Please make a clean machine to test again. I wonder if there is some other elements affect.

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Thursday, February 18, 2016 5:53 AM
  • The functionality of BitmapSource.CriticalCopyPixels() is offered by WindowsCodecs.dll. Since the DLL is updated with MS15-024 (Oct-2015) in Win7+, this could be an issue got fixed.

    Try to install it to see if it helps.


    Thursday, February 18, 2016 6:30 AM
    Answerer
  • Hi Kristin,

    Thank You for testing the solution and providing the screen recording.

    However in the animation i see that you chose an " document writer" as your printer. I assume its the XPS Document Writer. We also were able to use the XPS Document Writer to print the document without any problems (with or without KB2670838). As I mentioned in my question, any other than the XPS document writer (and it should not be a redirected one) should be chosen to reproduce the problem.

    Removing KB2670838 from the system caused the printed documents to partially contain empty pages (usually the first page of the printed document was white), so removing that update is not an option for us.

    I made a screen capture of the problem which can be found here.

    Thanks and best regards

    Peter

    Thursday, February 18, 2016 8:12 AM
  • Hi cheong00,

    Thanks for the hint. 

    I tried to print the document with and without KB3035132(MS15-024) installed on Windows 7. But the problem occured in both cases.

    Best regards

    Peter

    Thursday, February 18, 2016 8:17 AM
  • Hi Kristin,

    Thank You for testing the solution and providing the screen recording.

    However in the animation i see that you chose an " document writer" as your printer. I assume its the XPS Document Writer. We also were able to use the XPS Document Writer to print the document without any problems (with or without KB2670838). As I mentioned in my question, any other than the XPS document writer (and it should not be a redirected one) should be chosen to reproduce the problem.

    Removing KB2670838 from the system caused the printed documents to partially contain empty pages (usually the first page of the printed document was white), so removing that update is not an option for us.

    I made a screen capture of the problem which can be found here.

    Thanks and best regards

    Peter

    Hi Peter,

    Thanks for making a good screen capture to me.

    I am curious about why you use CutePDF printer. Do you mean this CutePDF printer? Sorry, I am never used that printer? Does it same as our normal printer?

    Could you try to test on a real printer and the XPS windows printer?

    Based on your error, I searched some, please check the following thread if it helps you.

    WPF Printing Large Report Causes OutOfMemoryException

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, February 19, 2016 7:00 AM
  • Hi Kristin,

    Thanks for the provided information. About Your question: Yes i am using the same printer you provided in your link. CutePDF is a commonly used virtual printer printing stuff into a pdf document.

    But the problem is the same using CutePDF or any other physical/real printer. And i did try to use the windows XPS printer... it works as i mentioned above. However its not a solution to print into an xps document and printing that xps document via XPS document viewer.

    Since the errors occur even on one-paged documents with small images, i would exclude the possibility of an OutOfMemory case.

    Best Regards

    Peter

    Friday, February 19, 2016 9:10 AM
  • Maybe you'll want to try the other virtual printers.

    Too bad you need to buy Adobe Acrobat to get Adobe Distiller.

    Also, I remember that some drivers for physical printer (Not remember whether it's HP or Epson one) has "Print to PostScript". If you're lucky to get that one, you can try to print it to postscript then use "ps2pdf" command from GhostScript utility to convert it to PDF too.
    Saturday, February 20, 2016 4:43 PM
    Answerer
  • I just come across something handy and want to add here, just in case someone else may need it.

    Publisher of MS Office (comes with Pro version or above) come with build in PS printing functionality. Here's how to set it:

    1) Go to "Administrative Tools" -> "Print Management". And add printer there.
    2) Select "File:" for port and click "Next".
    3) For driver, select "Generic" brand and then "MS Publisher Color Printer"

    That's it.

    When you try to print files to there, it'll prompt you for filename. When you save it and open it with text editor, you see the file begins with "%!PS-Adobe-3.0" line and know it's a PostScript file.

    Friday, March 4, 2016 8:35 AM
    Answerer