13

WPF 中如何创建忽略 DPI 属性的图片

 4 years ago
source link: https://blog.walterlv.com/post/create-wpf-image-source-ignoring-dpi.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

WPF 框架设计为与 DPI 无关,但你依然可能遇到 DPI 问题。尤其是 Image 控件显示的图片会根据图片 EXIF 中的 DPI 信息和屏幕 DPI 自动缩放图片。对于 UI 用图来说这是好事,但对于软件用户随便插入的图片来说就不是了——用户传入的图片可能是各种各样不统一的 DPI。因此这种 DPI 我们应该忽略。

解决方法

直接设置 Image 控件的大小是一个不错的方案,这在允许设置 Image 控件大小的场合下是可以使用的。如果你能设置,那么直接设置,这是最好的方法了。

除此之外,我们还可能可以尝试这些方法:

  1. 创建 BitmapImage 对象,根据当前屏幕的 DPI 值计算 DecodePixelWidthDecodePixelHeight
  2. 创建 DrawingImage 对象,直接按照 WPF 的坐标单位绘制图片原始像素大小的图片;
  3. 创建 Bitmap / WriteableBitmap 对象,重新创建一张 96 DPI 的图片。

以下的代码中,都假设当前 DPI 的值为 monitorDpi

DrawingImage

DrawingImage 可以使用 WPF 的方式来绘制,不过如果要绘制位图,也需要一个 BitmapImage 对象,不过这个时候我们可以按照我们需要的尺寸进行绘制而不用关心 DPI 的问题。由于尺寸是在绘制的时候确定的,所以不需要 Image 控件也设置尺寸。

private static ImageSource CreateBitmapImage(Stream sourceStream)
{
    var bitmap = new BitmapImage();
    bitmap.BeginInit();
    bitmap.StreamSource = sourceStream;
    bitmap.EndInit();

    var image = new ImageDrawing(
        bitmap,
        new Rect(0, 0, bitmap.PixelWidth / monitorDpi.FactorX, bitmap.PixelHeight / monitorDpi.FactorY));
    var drawing = new DrawingImage(image);
    return drawing;
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK