WPF:如何以原始大小显示图像? [英] WPF: How to display an image at its original size?

查看:1884
本文介绍了WPF:如何以原始大小显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在WPF中显示图片时遇到问题。

I have a problem with displaying images in 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,但似乎它不起作用。

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.

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

So, how can I fix this problem? Thank you!

推荐答案

这里是一个类似的问题。通常设置 Stretch =None就足够了。

Here is a similar question. Generally setting Stretch="None" is enough.

DPI拥有的内容也非常重要在元数据中设置图像。我花了很长时间才弄清楚如果图像的DPI与显示器的DPI(通常为96)不同, WPF会自动调整图像大小,因为它会尝试独立于DPI

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.

这篇关于WPF:如何以原始大小显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆