我在WPF中显示图像时遇到问题 .
这是我的代码:
<Button HorizontalAlignment="Left" Grid.Column="1" Grid.Row="5" Margin="0,5">
<Button.Content>
<StackPanel Orientation="Horizontal" Margin="10,0">
<Image Source="/images/user_add.png" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center" Width="24" Height="24" />
<TextBlock Text="添加" />
</StackPanel>
</Button.Content>
</Button>
我有一个原始大小为32 * 32的图像,但是当我运行上面的代码时,图像将拉伸以填充所有空间,超出其原始大小 . 我还将“Stretch”属性设置为“None”,但似乎它不起作用 .
那么,我该如何解决这个问题呢?谢谢!
4 回答
尽量不要指定宽度或高度,请改为使用它:
添加到Paya的答案:为了补偿
WPF
尝试适应显示器分辨率,您应该能够将Width
和Height
设置为文件的原始尺寸并使用Stretch="Fill"
. 这对我有用 .Here是一个类似的问题 . 一般设置
Stretch="None"
就足够了 .It is also very important what DPI has the image set in metadata. 我花了很长时间才弄清楚如果图像's DPI is different from the monitor'的DPI(通常是96),WPF will automatically resize the image, as it tries to be DPI-independent .
这个适合我,对于
600x800 pixels
和96dpi
的图像 .@ rishad2m8如果大小未知,我可以先用_2836181检测大小 . 我猜 .