RenderTargetBitmap呈现错误大小的图像

时间:2012-10-30 17:36:31

标签: c# wpf image thumbnails dpi

我的任务是向用户显示他的XPS文档的每个页面的缩略图。我需要所有图像都很小,所以我用 dpi 设置为72.0渲染它们(我用谷歌搜索了那张A4纸的尺寸,dpi 72.0是635x896)。基本上,我做了以下几点:

        List<BitmapImage> thumbnails = new List<BitmapImage>();
        documentPaginator.ComputePageCount();
        int pageCount = documentPaginator.PageCount;
        for (int i = 0; i < pageCount; i++)
        {
            DocumentPage documentPage = documentPaginator.GetPage(i);
            bool isLandscape = documentPage.Size.Width > documentPage.Size.Height;
            Visual pageVisual = documentPage.Visual;
            //I want all the documents to be less or equals to A4
            //private const double A4_SHEET_WIDTH = 635;
            //private const double A4_SHEET_HEIGHT = 896;
            //A4 sheet size in px, considering 72 dpi
            RenderTargetBitmap targetBitmap = new RenderTargetBitmap(
            (int)(System.Math.Min(documentPage.Size.Width, A4_SHEET_WIDTH)),
            (int)(System.Math.Min(documentPage.Size.Height, A4_SHEET_HEIGHT)),
            72.0, 72.0,
            PixelFormats.Pbgra32);
            targetBitmap.Render(pageVisual);
            BitmapFrame frame = BitmapFrame.Create(targetBitmap);
            PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(frame);
            BitmapImage image = new BitmapImage();
            using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
            {
                encoder.Save(ms);
                image.BeginInit();
                image.CacheOption = BitmapCacheOption.OnLoad;
                image.StreamSource = ms;
                if (isLandscape)
                {
                    image.Rotation = Rotation.Rotate270;
                }
                image.EndInit();
            }
            thumbnails.Add(image);
        }

但是当我渲染文档页面(A4)时,它的大小实际上是 846x1194 ,而不是我预期的那个。我试图让dpi降低(48.0)并且图像的大小变得更大(我想,我只是不太自然而且dpi是什么以及它如何影响文档)。我试图使 dpi = 96.0 ,并且尺寸变小了。我从上面的代码生成的类BitmapImage的实例集合中设置了一个图像作为Image控件的源(我正在创建一个WPF应用程序),如果dpi设置为96.0 my程序看起来像这样:

Thumbnail displayed incorrectly
正如您所看到的,页面的一部分根本没有显示,它不适合Image控件,即使控件的大小设置为 635x896 ,这就是为什么根据上面的代码,图像必须正确显示,所有文字必须适合 简而言之,我期望得到什么结果:我正在尝试创建文档页面的缩略图,但我希望它们相对于某些数字而言更小(对不起,我不太清楚我怎么说这样的东西在英文中,基本上如果文档的页面宽度是1200像素,我希望它是1200/n,其中 n 是我之前提到的“某个数字”,但是如果缩小图像的大小仍然大于 635x896 我希望尺寸 635x896
提前致谢。我也很抱歉我的英语不好。

1 个答案:

答案 0 :(得分:18)

首先,DPI表示每英寸点数,或每英寸像素数。如果将A4页面(21 x 29.7 cm)渲染到72 DPI的位图,最终会得到以下大小的位图:

  • 宽度= 21厘米/(2.54厘米/英寸)* 72像素/英寸= 595像素
  • 高度= 29.7厘米/(2.54厘米/英寸)* 72像素/英寸= 842像素

除此之外,您不应过多关注DPI,但有一个例外:WPF渲染在96 DPI完成。这意味着文档的A4大小的页面将呈现为794 x 1123位图。提醒一下:

  • 宽度= 21厘米/(2.54厘米/英寸)* 96像素/英寸= 794像素
  • 高度= 29.7厘米/(2.54厘米/英寸)* 96像素/英寸= 1123像素

因此,当它包含一个完全是A4的页面时,794 x 1123应该是RenderTargetBitmap的大小。如果页面大小小于A4,则位图应该更小。另一方面,如果页面大于A4,它应该按比例缩小到794 x 1123.这就是诀窍。您可以将视觉效果放入带有ContainerVisualScaleTransform,而不是将视觉直接渲染到RenderTargetBitmap中。

for (int i = 0; i < paginator.PageCount; i++)
{
    DocumentPage page = paginator.GetPage(i);
    double width = page.Size.Width;
    double height = page.Size.Height;
    double maxWidth = Math.Round(21.0 / 2.54 * 96.0); // A4 width in pixels at 96 dpi
    double maxHeight = Math.Round(29.7 / 2.54 * 96.0); // A4 height in pixels at 96 dpi
    double scale = 1.0;
    scale = Math.Min(scale, maxWidth / width);
    scale = Math.Min(scale, maxHeight / height);

    ContainerVisual containerVisual = new ContainerVisual();
    containerVisual.Transform = new ScaleTransform(scale, scale);
    containerVisual.Children.Add(page.Visual);

    RenderTargetBitmap bitmap = new RenderTargetBitmap(
        (int)(width * scale), (int)(height * scale), 96, 96, PixelFormats.Default);

    bitmap.Render(containerVisual);

    ...
}
相关问题