Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将XPS文件转换为高质量(而不是模糊的低分辨率)的图像?_C#_Wpf_Silverlight_Xps_Rendertargetbitmap - Fatal编程技术网

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

C# 如何将XPS文件转换为高质量(而不是模糊的低分辨率)的图像?,c#,wpf,silverlight,xps,rendertargetbitmap,C#,Wpf,Silverlight,Xps,Rendertargetbitmap,我正在尝试使用WPF转换XPS 我们的想法是可以使用silverlight 4加载这些图像,为此,我使用以下代码: // XPS Document XpsDocument xpsDoc = new XpsDocument(xpsFileName, System.IO.FileAccess.Read); FixedDocumentSequence docSeq = xpsDoc.GetFixedDocumentSequence();

我正在尝试使用WPF转换XPS

我们的想法是可以使用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();
//XPS文档
XpsDocument xpsDoc=新的XpsDocument(xpsFileName,System.IO.FileAccess.Read);
FixedDocumentSequence docSeq=xpsDoc.GetFixedDocumentSequence();
//页数
PageCount=docSeq.References[0]。GetDocument(false)。Pages.Count;
DocumentPage sizePage=docSeq.DocumentPaginator.GetPage(0);
PageHeight=sizePage.Size.Height;
PageWidth=sizePage.Size.Width;
//将尺寸从96 dpi缩放到600 dpi。
双刻度=300/96;
//将XPS页面转换为PNG文件
对于(int pageNum=0;pageNum
此代码取自转换XPS文档的项目。 很好! 但问题是图像分辨率低且模糊。 我研究并发现RenderTargetBitmap,并在此页面上找到:

这里的问题是您没有使用硬件渲染器目标位图渲染。

一种解决方案是将DirectX与WPF结合使用来实现这一点,但还没有找到任何清晰的示例来向我展示正确的方法

谢谢你的建议,谢谢

更新:我附加了XPS文档,我正在尝试转换图像
sourceforge.net上有一个名为xps2img的项目,该项目将xps转换为图像。它是用C#制作的,还包含源代码。请查看。它将帮助您实现所需的功能


我在这篇文章和其他许多文章中看到,人们在将DocumentPage转换为图像并将其保存在HDD上时遇到问题。 此方法从document viewer获取所有页面,并将它们作为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();
                }
            }
        }
    }
public void SaveDocumentPagesToImages(IDocumentPaginatorSource文档,字符串dirPath)
{
if(string.IsNullOrEmpty(dirPath))返回;
if(dirPath[dirPath.Length-1]!='\\')
dirPath+=“\\”;
如果(!Directory.Exists(dirPath))返回;
MemoryStream[]streams=null;
尝试
{
int pageCount=document.DocumentPaginator.pageCount;
DocumentPage[]pages=新文档页面[pageCount];
for(int i=0;i
基于xps2img的nuget软件包现已推出:

Api可在此处获得:

私有IList GetTifPagesFromXps(字符串xXpsFileName,双xQuality)
{
使用(var xpsDoc=newxpsdocument(xXpsFileName,FileAccess.Read))
{
var docSeq=xpsDoc.GetFixedDocumentSequence();
var tifPages=新列表();
对于(变量i=0;i    private IList<byte[]> GetTifPagesFromXps(string xXpsFileName, double xQuality)
    {
        using (var xpsDoc = new XpsDocument(xXpsFileName, FileAccess.Read))
        {
            var docSeq = xpsDoc.GetFixedDocumentSequence();

            var tifPages = new List<byte[]>();
            for (var i = 0; i < docSeq.DocumentPaginator.PageCount; i++)
            {
                using (var docPage = docSeq.DocumentPaginator.GetPage(i))
                {
                    var renderTarget = new RenderTargetBitmap((int)(docPage.Size.Width * xQuality), (int)(docPage.Size.Height * xQuality), 96 * xQuality, 96 * xQuality, PixelFormats.Default);

                    renderTarget.Render(docPage.Visual);

                    var jpegEncoder = new JpegBitmapEncoder { QualityLevel = 100 };
                    jpegEncoder.Frames.Add(BitmapFrame.Create(renderTarget));

                    byte[] buffer;
                    using (var memoryStream = new MemoryStream())
                    {
                        jpegEncoder.Save(memoryStream);
                        memoryStream.Seek(0, SeekOrigin.Begin);
                        buffer = memoryStream.GetBuffer();
                    }
                    tifPages.Add(buffer);
                }
            }

            xpsDoc.Close();
            return tifPages.ToArray();
        }
    }