WPF:按钮中奇怪的图像拉伸

发布于 2024-10-06 01:42:53 字数 1422 浏览 5 评论 0原文

我有多个按钮,每个按钮都有一个 32x32 像素的 PNG 图像。奇怪的是,两个按钮显示不同的尺寸(是的,我三次检查图标确实是 32x32!)。秒按钮看起来大小为 48x48 像素。最有趣的是,如果我省略 Stretch="None" 属性,图标会放大到几乎填满整个屏幕。

我无法向自己解释为什么会发生这种情况!

    <ToolBar Name="toolBar1" DockPanel.Dock="Top">
        <Button Name="importButton" ToolTip="Import" Click="importButton_Click">
            <Image Source="Icons/Import.png" Stretch="None" />
        </Button>
        <Button Name="toggleDetails" ToolTip="Details for Item" Click="toggleDetails_Click">
            <Image Source="Icons/maximize.png" Stretch="None" />
        </Button>           
    </ToolBar>

    <StackPanel Name="stackPanel1" DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,5,0,5">
        <Label  Name="label2" Content="Find"></Label>
        <TextBox  Name="tags" Width="400" KeyDown="tags_KeyDown" />
        <Button ToolTip="Find" Name="findItemsButton" Click="findItemsButton_Click">
            <Image Source="Icons/xmag.png" Stretch="None" />
        </Button>
        <CheckBox Content="Show Closed" Name="showClosedItemsCheckBox" VerticalAlignment="Center" Margin="10,0,0,0" Click="showClosedItemsCheckBox_Click" />
    </StackPanel>
    <TabControl  Name="tabControl" TabStripPlacement="Top">

    </TabControl>

</DockPanel>

I have multiple buttons, each having a 32x32 pixels PNG image. The strange thing is, that both buttons show different sizes (Yes, I triple checked that icons are really 32x32!). The seconds button looks like as it would be 48x48 pixels in size. The funniest thing is, if I omit the Stretch="None" attribute, the icons are scaled up to fill nearly the whole screen.

I cannot explain myself why this is happening!

    <ToolBar Name="toolBar1" DockPanel.Dock="Top">
        <Button Name="importButton" ToolTip="Import" Click="importButton_Click">
            <Image Source="Icons/Import.png" Stretch="None" />
        </Button>
        <Button Name="toggleDetails" ToolTip="Details for Item" Click="toggleDetails_Click">
            <Image Source="Icons/maximize.png" Stretch="None" />
        </Button>           
    </ToolBar>

    <StackPanel Name="stackPanel1" DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,5,0,5">
        <Label  Name="label2" Content="Find"></Label>
        <TextBox  Name="tags" Width="400" KeyDown="tags_KeyDown" />
        <Button ToolTip="Find" Name="findItemsButton" Click="findItemsButton_Click">
            <Image Source="Icons/xmag.png" Stretch="None" />
        </Button>
        <CheckBox Content="Show Closed" Name="showClosedItemsCheckBox" VerticalAlignment="Center" Margin="10,0,0,0" Click="showClosedItemsCheckBox_Click" />
    </StackPanel>
    <TabControl  Name="tabControl" TabStripPlacement="Top">

    </TabControl>

</DockPanel>

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

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

发布评论

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

评论(3

自此以后,行同陌路 2024-10-13 01:42:53

这两个图像可能具有不同的 DPI。

The two images probably have different DPIs.

给我一枪 2024-10-13 01:42:53

如果您为 Image 设置了 Stretch="None",但它看起来仍然更大/更小/模糊,那么可能是因为 DPI 不匹配。

例如,PNG 文件存储 DPI。 Windows 有特定的 DPI。检查您的系统 DPI 并检查 PNG DPI。

在 Photoshop 中,您可以转到 Image ->图像大小,它将显示点数/英寸框。您还可以使用它来更改 DPI。确保禁用Resample Image复选框,以便仅更改 DPI。您需要使用另存为Web对话框来保存该更改,因为普通的另存为不会保存该信息。

在此处输入图像描述

就我而言,我有一个大小为 24x24 的 PNG 文件,DPI 72.009,我的系统是在默认 DPI 下。图片看起来更大、更模糊,现在使用 Photoshop 将 PNG DPI 从 72.009 调整为 72 并使用另存为 Web 后就可以了。

If you have set Stretch="None" for the Image, and it still looks larger/smaller/blurry, then it's probably because of the DPI mismatch.

For example, PNG files store DPI. Windows has a particular DPI. Check your system DPI and check the PNG DPI.

In Photoshop you can go to Image -> Image Size and it will display the dots/inch box. You can also use it to change the DPI. Make sure you disable Resample Image checkbox so that you only alter the DPI. You need to use the Save for Web dialog for saving that change because the normal Save As won't save that information.

enter image description here

In my case, I had a PNG file with size of 24x24, and a DPI 72.009 and my system is at the default DPI. The picture looked larger and blurrier, now it's fine after adjusting the PNG DPI from 72.009 to 72 with Photoshop and using the Save for Web.

归属感 2024-10-13 01:42:53

要跟进 SLAks 答案:要使用图像的像素尺寸(无论 DPI 如何),您可以将宽度和高度绑定到源的 PixelWidth 和 PixelHeight,如下所示

<Button Name="toggleDetails" ToolTip="Details for Item" Click="toggleDetails_Click">  
    <Image Source="Icons/maximize.png"
           Stretch="None"
           Width="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelWidth}"
           Height="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelHeight}"/>
</Button>

To follow up on SLaks answer: To use the Pixel dimensions of the Image regardless of the DPI you can bind the Width and Height to the PixelWidth and PixelHeight of the Source like this

<Button Name="toggleDetails" ToolTip="Details for Item" Click="toggleDetails_Click">  
    <Image Source="Icons/maximize.png"
           Stretch="None"
           Width="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelWidth}"
           Height="{Binding RelativeSource={RelativeSource Self}, Path=Source.PixelHeight}"/>
</Button>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文