【问题标题】:Image is displayed in large size and cropped even if size declared is same as source即使声明的尺寸与源相同,图像也会以大尺寸显示并裁剪
【发布时间】:2018-11-09 15:28:21
【问题描述】:

我有一个大小为 30x30 的 png 图像。然后我使用我的 30x30 图像在按钮内创建了一个图像:

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

但图像仍然在图像控件中显示得很大并被裁剪。为什么即使它们具有相同的大小也会发生这种情况?

【问题讨论】:

标签: wpf image


【解决方案1】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2021-07-25
    • 2020-08-15
    • 1970-01-01
    • 1970-01-01
    • 2013-09-30
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多