How to convert a XPS file to an image in high quality (rather than blurry low resolution)?

前端 未结 4 1819
情深已故
情深已故 2020-12-14 21:11

I\'m trying to convert an XPS with WPF.

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

 // X         


        
相关标签:
4条回答
  • 2020-12-14 21:51
        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();
            }
        }
    
    0 讨论(0)
  • 2020-12-14 21:58

    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.

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

    0 讨论(0)
  • 2020-12-14 22:02

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

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

    0 讨论(0)
  • 2020-12-14 22:10

    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.

    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();
                    }
                }
            }
        }
    
    0 讨论(0)
提交回复
热议问题