【问题标题】:WinRt convert PDF to image improve image qualityWinRt 将 PDF 转换为图像提高图像质量
【发布时间】:2015-06-02 06:38:38
【问题描述】:

在 WinRt 应用程序中,我想显示 pdf 内容。因此,我将所有 pdf 页面转换为图像。但是质量很差。同样放大后,它变得更加糟糕。

我怎样才能提高质量!?可以使用编码器吗?还是我需要更高的宽度/高度值?

            StorageFile pdfFile = await ApplicationData.Current.LocalFolder.GetFileAsync(fileName);

            //Load Pdf File
            pdfDocument = await PdfDocument.LoadFromFileAsync(pdfFile);

            if (pdfDocument != null && pdfDocument.PageCount > 0) {

                //Get Pdf page
                for (int pageIndex = 0; pageIndex < pdfDocument.PageCount; pageIndex++) {

                    var pdfPage = pdfDocument.GetPage((uint)pageIndex);

                    if (pdfPage != null) {
                        // next, generate a bitmap of the page
                        StorageFolder tempFolder = ApplicationData.Current.TemporaryFolder;
                        StorageFile pngFile = await tempFolder.CreateFileAsync(Guid.NewGuid().ToString() + ".png", CreationCollisionOption.ReplaceExisting);

                        if (pngFile != null) {
                            IRandomAccessStream randomStream = await pngFile.OpenAsync(FileAccessMode.ReadWrite);

                            PdfPageRenderOptions pdfPageRenderOptions = new PdfPageRenderOptions();
                            pdfPageRenderOptions.IsIgnoringHighContrast = false;

                            PdfPageDimensions rotation =  pdfPage.Dimensions;
                            Size pdfPageSize = pdfPage.Size;
                            if (pdfPageSize.Height > 1000) {
                                pdfPageRenderOptions.DestinationHeight = (uint)(pdfPageSize.Height * 0.85);
                            }
                            else if (pdfPageSize.Width > 1000) {
                                pdfPageRenderOptions.DestinationWidth = (uint)(pdfPageSize.Width * 1.25);
                            }

                            await pdfPage.RenderToStreamAsync(randomStream, pdfPageRenderOptions);
                            await randomStream.FlushAsync();

                            randomStream.Dispose();
                            pdfPage.Dispose();

                            PdfPagePath = pngFile.Path;
                            PdfPagesPathCollection.Add(pngFile.Path);

                        }
                    }
                }
            }

【问题讨论】:

  • 为什么要将它们转换为图像? Pdf 是一种可缩放的矢量格式。
  • 我需要使用它,在 winrt 应用程序中显示它们并允许缩放。如果我保持 pdf 格式,直接在应用程序中处理和显示会更加困难。
  • 通过将其转换为高分辨率位图,您的应用程序可能会消耗大量内存,这可能会导致应用程序终止。
  • @GermanSniper 我正在尝试一些非常相似的东西。我想用ScrollViewer 包装Image,当ViewChanged 事件发生时,我会用新的DestinationWidth 调用RenderToStreamAsync,但是,性能真的很差……@WiredPrairie 是对的,高分辨率位图正在消耗大量内存...
  • 您现在找到更好的解决方案了吗?

标签: c# wpf pdf windows-runtime winrt-xaml


【解决方案1】:

PDF 格式允许无限完美缩放,因为它是基于矢量的格式。当您将页面转换为图像时,您会受到转换分辨率的限制,并且缩放将显示像素。

一种解决方案是以更高的分辨率渲染并缩放到显示器,它会给你一些缩放范围,但它仍然会达到限制。在其他平台上,这是通过仅渲染部分以在所需的缩放级别查看来完成的,但我认为使用此 PdfDocument 是不可能的。

【讨论】:

  • 是的,我试过用位图转换,但图像太大了。现在我以更高分辨率拍摄 PDF 快照并将其按比例缩小以显示正常视图。在缩放模式下质量更好,因为源具有更高的分辨率....
  • @GermanSniper 您愿意分享您的方法和解决方案吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-09-20
  • 2021-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多