views:

326

answers:

0

I have the following code, taken from http://www.codeplex.com/XPS2Image/ ,which in turn was taken from some people discussing at at Microsoft development forums.

    int[] pages = new int[] { 0, 1, 2, 3, 4 };
        XpsDocument xpsDoc = new XpsDocument(@"c:\tmp\sample.xps", System.IO.FileAccess.Read);
        FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();

        // You can get the total page count from docSeq.PageCount


        foreach (int pageNum in pages)
        {
            DocumentPaginator paginator = docSeq.DocumentPaginator;
            DocumentPage docPage = paginator.GetPage(pageNum);
            BitmapImage bitmap = new BitmapImage();
            RenderTargetBitmap renderTarget =
                new RenderTargetBitmap((int)docPage.Size.Width,
                                        (int)docPage.Size.Height,
                                        96, // WPF (Avalon) units are 96dpi based

                                        96,
                                        System.Windows.Media.PixelFormats.Pbgra32);

            renderTarget.Render(docPage.Visual);

            PngBitmapEncoder encoder = new PngBitmapEncoder();  // Choose type here ie: JpegBitmapEncoder, etc

            encoder.Frames.Add(BitmapFrame.Create(renderTarget));

            FileStream pageOutStream = new FileStream("c:\\tmp\\xpsdocPage" + pageNum + ".jpg", FileMode.Create, FileAccess.Write);
            encoder.Save(pageOutStream);
            pageOutStream.Close();
        }

This works fine, except Remote ResourceDictionaries used in the FixedPage are not rendered. Any ideas?