1

I have a preposterously large page (tens of thousands of pixels high and across, with tens of thousands of nodes and links) on an XPS document that I am attempting to convert to an image. The XPS document contains only one page.

On researching how to do this, the basic way of going about this (mostly based on other StackOverflow questions) seems to be something along the lines of:

[STAThread]
static void Main(string[] args)
{
    XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read);
    FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();
    DocumentPage page = docSeq.DocumentPaginator.GetPage(0);

    RenderTargetBitmap renderTarget = new RenderTargetBitmap(   (int)page.Size.Width,
                                                                (int)page.Size.Height,
                                                                96,
                                                                96,
                                                                PixelFormats.Default);

    renderTarget.Render(page.Visual); //The error occurs here
}

I haven't included any code for the actual image encoding and file creation after that Render call, because it fails at that point.

I don't get an out of memory exception, because I'm building it as a 64 bit application as I do realise that this operation requires a sizeable chunk of memory. Memory on the build machine isn't a problem.

The error I do get is a System.OverflowException, stating:

The image data generated an overflow during processing.

In addition, part-way through I get a 'ContextSwitchDeadlock' message, stating:

The CLR has been unable to transition from COM context 0xfc55d4d8 to COM context 0xfc55d600 for 60 seconds. The thread that owns the destination context/apartment is most likely either doing a non pumping wait or processing a very long running operation without pumping Windows messages. This situation generally has a negative performance impact and may even lead to the application becoming non responsive or memory usage accumulating continually over time. To avoid this problem, all single threaded apartment (STA) threads should use pumping wait primitives (such as CoWaitForMultipleHandles) and routinely pump messages during long running operations.

I'm not sure what I can do about this, as it disappears into code that I haven't written, I can't see how I can address this.

My question is basically, is there any way I can convert an XPS document with one or more enormous pages to say, a PNG image file? One thing I've tried to look into is using the RenderTargetBitmap to only render small chunks of the XPS page, and then join all the chunks together at the end into one image, however I haven't been able to find out how to do this, or if it's even possible.

Interminable
  • 1,338
  • 3
  • 21
  • 52

0 Answers0