WPF:如何以原始大小显示图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3055550/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF: How to display an image at its original size?
提问by jiluo
I have a problem with displaying images in WPF.
我在 WPF 中显示图像时遇到问题。
Here's my code:
这是我的代码:
<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>
I have an image with original size 32*32, but when I ran the above code, the image will stretch to fill all the space, beyond its original size. I also set the "Stretch" property to "None", but it seems that it doesn't work.
我有一个原始大小为 32*32 的图像,但是当我运行上面的代码时,图像将拉伸以填充所有空间,超出其原始大小。我还将“Stretch”属性设置为“None”,但似乎不起作用。
So, how can I fix this problem? Thank you!
那么,我该如何解决这个问题?谢谢!
回答by Paya
Hereis a similar question. Generally setting Stretch="None"
is enough.
这是一个类似的问题。一般设置Stretch="None"
就够了。
It is also very important what DPI has the image set in metadata.It took me quite a while before figuring out that if the image's DPI is different from the monitor's DPI (usually 96), WPF will automatically resize the image, as it tries to be DPI-independent.
DPI 在元数据中设置的图像也很重要。我花了很长时间才弄清楚如果图像的 DPI 与显示器的 DPI(通常为 96)不同,WPF 将自动调整图像大小,因为它试图与 DPI 无关。
EDIT
编辑
The MSDN link is broken, here is the new link: MSDN Blog - Blurry Bitmaps. Let's keep the old link around to be used for archive.org, in case the new link stops working also.
MSDN 链接已损坏,这是新链接: MSDN 博客 - 模糊位图。让我们保留旧链接以用于 archive.org,以防新链接也停止工作。
回答by Ulrich-Lorenz Schlüter
<Image Source="Images/Background.png" UseLayoutRounding="True" SnapsToDevicePixels="True" Width="600" Height="800" Stretch="Fill" />
This one works for me, for an image with 600x800 pixels
and 96dpi
.
这个对我有用,用于带有600x800 pixels
和的图像96dpi
。
@rishad2m8 If size is unknown one can detect the size first with https://msdn.microsoft.com/en-us/library/system.drawing.image.size(v=vs.110).aspxI'd guess.
@rishad2m8 如果大小未知,可以首先使用https://msdn.microsoft.com/en-us/library/system.drawing.image.size(v=vs.110).aspx检测大小,我猜。
回答by VoodooChild
Try not specifying width or height, use it like this instead:
尝试不指定宽度或高度,而是像这样使用它:
<Image Source="/images/user_add.png" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center" />
回答by sorrymissHymanson
Adding to Paya's answer: to compensate WPF
's attempt to adapt to the monitors resolution you should be able to set the Width
and Height
to the file's original dimensions and use Stretch="Fill"
. This worked for me.
添加到 Paya 的答案:为了补偿WPF
尝试适应显示器分辨率,您应该能够将Width
和Height
设置为文件的原始尺寸并使用Stretch="Fill"
. 这对我有用。
回答by shirley
If you want to display the image with original size, but don't know the image size, I think the best way is to set the image as background of UIElement. Like this:
如果你想以原始大小显示图像,但不知道图像大小,我认为最好的方法是将图像设置为UIElement的背景。像这样:
<Button>
<Button.Background>
<ImageBrush ImageSource="/images/user_add.png" Stretch="None"/>
</Button.Background>
</Button>