Cra*_*ker 5 c# wpf image thumbnails dpi
我的任务是向用户显示他的XPS文档的每个页面的缩略图.我需要所有的图像都很小,所以我用dpi设置为72.0 渲染它们(我用Google搜索了那张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);
}
Run Code Online (Sandbox Code Playgroud)
但是当我渲染文档页面(A4)时,它的大小实际上是846x1194而不是我预期的那个.我试图让dpi降低(48.0)并且图像的大小变得更大(我想,我只是不太自然而且dpi是什么以及它如何影响文档).我试图使dpi = 96.0,并且尺寸变小了.我从BitmapImage上面的代码生成的类的实例集合中设置了一个图像作为Image控件的源(我正在创建一个WPF应用程序),如果dpi设置为96.0,我的程序看起来像这样:

正如您所看到的,页面的一部分根本没有显示,它不适合Image控件,即使控件的大小设置为635x896,这就是为什么根据上面的代码,图像必须正确显示并且所有文字必须适合.
简而言之,我期望得到什么结果:我正在尝试创建文档页面的缩略图,但我希望它们相对于某些数字更小(对不起,我不太清楚我怎么说这样的东西在英文中,基本上如果文件的页面宽度是1200像素,我希望它是1200/n,其中n是我之前提到的"某个数字",但如果缩小图像的大小仍然大于635x896,我希望大小为635x896.
提前致谢.我也很抱歉我的英语不好.
Cle*_*ens 18
首先,DPI表示每英寸点数,或每英寸像素数.如果将A4页面(21 x 29.7 cm)渲染到72 DPI的位图,最终会得到以下大小的位图:
除此之外,您不应过多关注DPI,但有一个例外:WPF渲染是在96 DPI下完成的.这意味着文档的A4大小的页面将呈现为794 x 1123位图.提醒一句:
因此,当它包含一个完全是A4的页面时,794 x 1123应该是RenderTargetBitmap的大小.如果页面大小小于A4,则位图应该更小.另一方面,如果页面大于A4,则应缩小到794 x 1123.这就是诀窍.您可以将视觉效果放入带有ScaleTransform的ContainerVisual,而不是将页面直接渲染到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);
...
}
Run Code Online (Sandbox Code Playgroud)