如何将XPS文件转换为高质量的图像(而不是模糊的低分辨率)?

时间:2021-09-27 21:21:06

I'm trying to convert an XPS with WPF.

我正在尝试使用WPF转换XPS。

The idea is that these images can be loaded with silverlight 4, for this I am using the following code:

想法是这些图像可以加载silverlight 4,为此我使用以下代码:

 // XPS Document
            XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read);
            FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();

        // The number of pages
        PageCount = docSeq.References[0].GetDocument(false).Pages.Count;

        DocumentPage sizePage = docSeq.DocumentPaginator.GetPage(0);
        PageHeight = sizePage.Size.Height;
        PageWidth = sizePage.Size.Width;
        // Scale dimensions from 96 dpi to 600 dpi.
        double scale = 300/ 96;

        // Convert a XPS page to a PNG file
        for (int pageNum = 0; pageNum < PageCount; pageNum++)
        {
            DocumentPage docPage = docSeq.DocumentPaginator.GetPage(pageNum);
            BitmapImage bitmap = new BitmapImage();
            RenderTargetBitmap renderTarget =
                new RenderTargetBitmap((int)(scale * (docPage.Size.Height + 1)),
                                                               (int)(scale * (docPage.Size.Height + 1)),
                                                               scale * 96,
                                                               scale * 96, PixelFormats.Pbgra32);
            renderTarget.Render(docPage.Visual);


            PngBitmapEncoder encoder = new PngBitmapEncoder();

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

            FileStream pageOutStream = new FileStream(name + ".Page" + pageNum + ".png", FileMode.Create, FileAccess.Write);
            encoder.Save(pageOutStream);
            pageOutStream.Close();

This code is taken from http://xpsreader.codeplex.com/ a project to convert an XPS document. works great! But the problem is that the image is low resolution and blurry. I researched and found that RenderTargetBitmap and find on this page: http://www.codeproject.com/Questions/213737/Render-target-bitmap-quality-issues

此代码取自http://xpsreader.codeplex.com/转换XPS文档的项目。效果很好!但问题是图像分辨率低且模糊。我研究并发现RenderTargetBitmap并在此页面上找到:http://www.codeproject.com/Questions/213737/Render-target-bitmap-quality-issues

The issue here is you Have That does not use hardware RenderTargetBitmap rendering.

这里的问题是你有没有使用硬件RenderTargetBitmap渲染。

One solution is to use DirectX with WPF to do this, but have not found any clear example to show me the right way to do it.

一种解决方案是使用带有WPF的DirectX来执行此操作,但是没有找到任何明确的示例来向我展示正确的方法。

I appreciate suggestions. Thanks in advance.

我很感激建议。提前致谢。

Update:I attached the XPS document, I am trying to convert the image Please download test.xps

更新:我附加了XPS文档,我正在尝试转换图像请下载test.xps

3 个解决方案

#1


4  

I saw in this post and in many others that peoples have problems with conversion of DocumentPage to Image and saving it on HDD. This method took all pages from document viewer and save them on HDD as jpg images.

我在这篇文章和其他许多文章中看到人们在将DocumentPage转换为Image并将其保存在HDD上时遇到问题。此方法从文档查看器中获取所有页面,并将其作为jpg图像保存在HDD上。

public void SaveDocumentPagesToImages(IDocumentPaginatorSource document, string dirPath)
    {
        if (string.IsNullOrEmpty(dirPath)) return;

        if (dirPath[dirPath.Length - 1] != '\\')
            dirPath += "\\";

        if (!Directory.Exists(dirPath)) return;

        MemoryStream[] streams = null;
        try
        {
            int pageCount = document.DocumentPaginator.PageCount;
            DocumentPage[] pages = new DocumentPage[pageCount];
            for (int i = 0; i < pageCount; i++)
                pages[i] = document.DocumentPaginator.GetPage(i);

            streams = new MemoryStream[pages.Count()];

            for (int i = 0; i < pages.Count(); i++)
            {
                DocumentPage source = pages[i];
                streams[i] = new MemoryStream();

                RenderTargetBitmap renderTarget =
                   new RenderTargetBitmap((int)source.Size.Width,
                                           (int)source.Size.Height,
                                           96, // WPF (Avalon) units are 96dpi based
                                           96,
                                           System.Windows.Media.PixelFormats.Default);

                renderTarget.Render(source.Visual);

                JpegBitmapEncoder encoder = new JpegBitmapEncoder();  // Choose type here ie: JpegBitmapEncoder, etc
                encoder.QualityLevel = 100;
                encoder.Frames.Add(BitmapFrame.Create(renderTarget));

                encoder.Save(streams[i]);

                FileStream file = new FileStream(dirPath + "Page_" + (i+1) + ".jpg", FileMode.CreateNew);
                file.Write(streams[i].GetBuffer(), 0, (int)streams[i].Length);
                file.Close();

                streams[i].Position = 0;
            }
        }
        catch (Exception e1)
        {
            throw e1;
        }
        finally
        {
            if (streams != null)
            {
                foreach (MemoryStream stream in streams)
                {
                    stream.Close();
                    stream.Dispose();
                }
            }
        }
    }

#2


5  

There is a project named xps2img on sourceforge.net which converts an xps to image. It is made in C# and also contains the source code. Check it out. It will help you to achieve what you want.

sourceforge.net上有一个名为xps2img的项目,它将xps转换为图像。它是用C#制作的,还包含源代码。一探究竟。它将帮助您实现您想要的。

http://sourceforge.net/projects/xps2img/files/

http://sourceforge.net/projects/xps2img/files/

#3


0  

A nuget package based on xps2img is now available: https://www.nuget.org/packages/xps2img/

现在提供基于xps2img的nuget包:https://www.nuget.org/packages/xps2img/

Api available here: https://github.com/peters/xps2img#usage

这里有Api:https://github.com/peters/xps2img#usage

#1


4  

I saw in this post and in many others that peoples have problems with conversion of DocumentPage to Image and saving it on HDD. This method took all pages from document viewer and save them on HDD as jpg images.

我在这篇文章和其他许多文章中看到人们在将DocumentPage转换为Image并将其保存在HDD上时遇到问题。此方法从文档查看器中获取所有页面,并将其作为jpg图像保存在HDD上。

public void SaveDocumentPagesToImages(IDocumentPaginatorSource document, string dirPath)
    {
        if (string.IsNullOrEmpty(dirPath)) return;

        if (dirPath[dirPath.Length - 1] != '\\')
            dirPath += "\\";

        if (!Directory.Exists(dirPath)) return;

        MemoryStream[] streams = null;
        try
        {
            int pageCount = document.DocumentPaginator.PageCount;
            DocumentPage[] pages = new DocumentPage[pageCount];
            for (int i = 0; i < pageCount; i++)
                pages[i] = document.DocumentPaginator.GetPage(i);

            streams = new MemoryStream[pages.Count()];

            for (int i = 0; i < pages.Count(); i++)
            {
                DocumentPage source = pages[i];
                streams[i] = new MemoryStream();

                RenderTargetBitmap renderTarget =
                   new RenderTargetBitmap((int)source.Size.Width,
                                           (int)source.Size.Height,
                                           96, // WPF (Avalon) units are 96dpi based
                                           96,
                                           System.Windows.Media.PixelFormats.Default);

                renderTarget.Render(source.Visual);

                JpegBitmapEncoder encoder = new JpegBitmapEncoder();  // Choose type here ie: JpegBitmapEncoder, etc
                encoder.QualityLevel = 100;
                encoder.Frames.Add(BitmapFrame.Create(renderTarget));

                encoder.Save(streams[i]);

                FileStream file = new FileStream(dirPath + "Page_" + (i+1) + ".jpg", FileMode.CreateNew);
                file.Write(streams[i].GetBuffer(), 0, (int)streams[i].Length);
                file.Close();

                streams[i].Position = 0;
            }
        }
        catch (Exception e1)
        {
            throw e1;
        }
        finally
        {
            if (streams != null)
            {
                foreach (MemoryStream stream in streams)
                {
                    stream.Close();
                    stream.Dispose();
                }
            }
        }
    }

#2


5  

There is a project named xps2img on sourceforge.net which converts an xps to image. It is made in C# and also contains the source code. Check it out. It will help you to achieve what you want.

sourceforge.net上有一个名为xps2img的项目,它将xps转换为图像。它是用C#制作的,还包含源代码。一探究竟。它将帮助您实现您想要的。

http://sourceforge.net/projects/xps2img/files/

http://sourceforge.net/projects/xps2img/files/

#3


0  

A nuget package based on xps2img is now available: https://www.nuget.org/packages/xps2img/

现在提供基于xps2img的nuget包:https://www.nuget.org/packages/xps2img/

Api available here: https://github.com/peters/xps2img#usage

这里有Api:https://github.com/peters/xps2img#usage