WPF:如何以原始大小显示图像?

我有一个在WPF中显示图像的问题。

这是我的代码:

<Button HorizontalAlignment="Left" Grid.Column="1" Grid.Row="5" Margin="0,5"> <Button.Content> <StackPanel Orientation="Horizontal" Margin="10,0"> <Image Source="http://img.dovov.comuser_add.png" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center" Width="24" Height="24" /> <TextBlock Text="添加" /> </StackPanel> </Button.Content> </Button> 

我有一个原始大小为32 * 32的图像,但是当我运行上面的代码时,图像将伸展以填充所有空间,超出其原始大小。 我也将“拉伸”属性设置为“无”,但似乎不起作用。

那么,我该如何解决这个问题呢? 谢谢!

这是一个类似的问题。 一般设置Stretch="None"就足够了。

DPI在元数据中设置图像也是非常重要的。 我花了相当长的时间才发现,如果图像的DPI与显示器的DPI(通常为96)不同,那么WPF将自动调整图像大小,因为它试图独立于DPI 。

尝试不指定宽度或高度,而是像这样使用它:

 <Image Source="http://img.dovov.comuser_add.png" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center" /> 
 <Image Source="Images/Background.png" UseLayoutRounding="True" SnapsToDevicePixels="True" Width="600" Height="800" Stretch="Fill" /> 

这个对我来说是一个600x800 pixels96dpi的图像。

@ rishad2m8如果大小是未知的,可以用https://msdn.microsoft.com/en-us/library/system.drawing.image.size(v=vs.110).aspx首先检测大小。

添加到Paya的答案:为了弥补WPF的尝试,以适应显示器的分辨率,你应该能够设置文件的原始尺寸的WidthHeight ,并使用Stretch="Fill" 。 这对我有效。