为什么此位图图像在加载后会更改其大小? [英] Why this Bitmap Image changes its size after I load it?

查看:58
本文介绍了为什么此位图图像在加载后会更改其大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

快速提问:

我有这张 1000 x 1000 位图图像:

并且我使用此例程来加载它:

  private BitmapSource initialBitmap = new BitmapImage(new Uri("C:\\ Users \\ ... \\ Desktop \\ Original.bmp"));; 

为什么在加载后以及越过上述行之后立即将其显示为 800 x 800 ?

P.S我希望它是1000 x 1000,并且不使用任何调整大小功能.它正在工作,突然变成800 * 800了!

解决方案

BitmapSource.Height 不是以像素为单位,而是WPF的设备独立单位,始终为96 dpi.例如:

以与设备无关的单位(每单位1/96英寸)获取位图的宽度.

如果您想知道实际的像素宽度和高度,则需要使用 PixelHeight 属性.

您的问题不是很具体,但是如果您真正关心的是将位图显示为与创作时相同的大小,那么最简单的解决方案是确保以96 dpi进行创作.无论您使用什么程序创作位图,都可能在某个位置设置位图分辨率.通常,可以在更改或不更改图片的像素尺寸(即放大或缩小图片)的情况下进行设置;您想要在不缩放图像的情况下执行此操作,以使像素尺寸保持不变,但dpi会发生变化以匹配WPF所使用的图像.

请注意,这仍然不能保证位图以特定的像素大小显示.显示分辨率可以并且通常不同于96 dpi,在这种情况下,WPF将缩放图像以确保图像的物理尺寸(即,以英寸,毫米等为单位的尺寸)根据位图中的信息是正确的.例如,以96 dpi的分辨率显示960像素宽意味着10英寸宽.在120 dpi的显示屏上,这意味着显示足够大的位图,以便其宽度使用1200个显示像素.

如果希望或需要位图以完全相同的显示像素数显示,而不管显示分辨率如何,则必须设置一个变换,在其中显示图像,以逆转WPF会产生的缩放效果否则做.当然,这需要知道显示分辨率.


这是一些其他相关的堆栈溢出问题,您可能会发现它们很有用: RenderTargetBitmap呈现尺寸错误的图像
用于LCD屏幕全高清的WPF
WPF中的屏幕分辨率问题?

Quick question:

I have this 1000 x 1000 bitmap image:

and I use this routine to load it:

private BitmapSource initialBitmap = new BitmapImage(new Uri("C:\\Users\\...\\Desktop\\Original.bmp"));

Why after I load it, and right after I step over the above line, I see it as 800 x 800?

P.S I want it to be 1000 x 1000 and without using any Resize functions. It was working and suddenly it is 800*800 !

解决方案

The values returned by BitmapSource.Width and BitmapSource.Height are not in pixels, but rather WPF's device-independent units, which are always 96 dpi. E.g.:

Gets the width of the bitmap in device-independent units (1/96th inch per unit).

If you want to know the actual pixel width and height, you need to use the PixelWidth and PixelHeight properties.

Your question isn't very specific, but if what you are actually concerned about is having the bitmap display at the same size in which it was authored, then the easiest solution is to make sure you author it at 96 dpi. Whatever program you're using to author the bitmap likely has a place where you can set the bitmap resolution. Typically this can be set with or without changing the pixel dimensions of the image (i.e. scaling the image larger or smaller); you want to do it without scaling the image, so that the pixel dimensions remain the same but the dpi changes to match what WPF is using.

Note that this still won't guarantee the bitmap displays at a specific pixel size. The display resolution can be and often is different from 96 dpi, in which case WPF will scale images to ensure that the physical dimensions of the image (i.e. the dimensions in inches, millimeters, etc.) are correct according to the information in the bitmap. For example, 960 pixels wide at 96 dpi means 10" wide. On a 120 dpi display, this means displaying the bitmap large enough so that its width uses 1200 display pixels.

If you want or need the bitmap to display at exactly the same number of display pixels regardless of the display resolution, then you'll have to set a transform where you display the image to reverse the effect of the scaling that WPF would otherwise do. This requires knowing the display resolution, of course.


Here are some other related Stack Overflow questions which you might find useful: RenderTargetBitmap renders image of a wrong size
WPF for LCD screen Full HD
Screen Resolution Problem In WPF?

这篇关于为什么此位图图像在加载后会更改其大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆