首页 文章

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

提问于
浏览
91

我在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 回答

  • 7

    尽量不要指定宽度或高度,请改为使用它:

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

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

  • 120

    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 .

  • 3
    <Image Source="Images/Background.png" UseLayoutRounding="True" SnapsToDevicePixels="True" Width="600" Height="800" Stretch="Fill" />
    

    这个适合我,对于 600x800 pixels96dpi 的图像 .

    @ rishad2m8如果大小未知,我可以先用_2836181检测大小 . 我猜 .

相关问题