将固定页面内容渲染到位图上

发布于 2024-07-14 04:23:51 字数 1633 浏览 6 评论 0原文

我有以下代码取自 http://www.codeplex.com/XPS2Image/ ,其中依次摘自 Microsoft 开发人员网络论坛上的讨论。

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

这工作正常,只是 FixedPage 中使用的远程 ResourceDictionaries 未呈现。 有任何想法吗?

I have the following code taken from http://www.codeplex.com/XPS2Image/, which in turn was taken from a discussion on the Microsoft Developer's Network 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 that remote ResourceDictionaries used in the FixedPage are not rendered. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文