即使声明的尺寸与来源相同,图像也会以大尺寸显示并被裁剪

时间:2018-11-09 06:23:24

标签: wpf image

我有一个尺寸为30x30的png图片。然后,我使用30x30的图片在按钮内创建了图片:

<Button>
  <Image x:Name="Sample" Source="sample.png" Stretch="None" SnapsToDevicePixels="True" 
         Width="30" Height="30" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Button>

但是,图像仍然显示为大尺寸并在“图像”控件中被裁剪。为什么即使它们大小相同也会发生这种情况?

1 个答案:

答案 0 :(得分:1)

sample.png中的图像的DPI可能不同于96,而WPF则将其用作device independent units的大小。

请不要在Image元素上设置Stretch="None"以便正确缩放。 Stretch的默认值为Uniform

<Image Source="sample.png" Width="30" Height="30"/>

如果将图像加载到BitmapImage中并将其WidthHeight与它的PixelWidth进行比较,则可以检查图像的原始大小和(未拉伸的)渲染大小之间的差异。和PixelHeight