WPF:如何以原始尺寸显示图像?

发布于 2024-09-06 09:27:24 字数 662 浏览 6 评论 0 原文

我在 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 a problem with displaying images in 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.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

青朷 2024-09-13 09:27:24

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

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


编辑

MSDN 链接已损坏,以下是新链接:
MSDN 博客 - 模糊位图。让我们保留旧链接以用于 archive.org,以防新链接也停止工作。

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

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.


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.

拧巴小姐 2024-09-13 09:27:24

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

<Image Source="/images/user_add.png" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center" />

Try not specifying width or height, use it like this instead:

<Image Source="/images/user_add.png" Stretch="None" HorizontalAlignment="Center" VerticalAlignment="Center" />
守望孤独 2024-09-13 09:27:24
<Image Source="Images/Background.png" UseLayoutRounding="True" SnapsToDevicePixels="True" Width="600" Height="800" Stretch="Fill" />

这个适用于我,适用于 600x800 像素96dpi 的图像。

@rishad2m8如果尺寸未知,可以首先使用 https://msdn.microsoft.com/en-us/library/system.drawing.image.size(v=vs.110).aspx 我猜。

<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.

@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).aspx I'd guess.

一影成城 2024-09-13 09:27:24

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

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.

机场等船 2024-09-13 09:27:24

如果你想以原始大小显示图像,但不知道图像大小,我认为最好的方法是将图像设置为 UIElement 的背景。像这样:

    <Button Height="100" Width="100">
        <Button.Background>
            <ImageBrush ImageSource="/images/user_add.png" Stretch="None"/>
        </Button.Background>
    </Button>

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:

    <Button Height="100" Width="100">
        <Button.Background>
            <ImageBrush ImageSource="/images/user_add.png" Stretch="None"/>
        </Button.Background>
    </Button>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文