WPF:按钮中奇怪的图像拉伸
我有多个按钮,每个按钮都有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这两个图像可能具有不同的 DPI。
The two images probably have different DPIs.
如果您为
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 theImage
, 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 disableResample Image
checkbox so that you only alter the DPI. You need to use theSave for Web
dialog for saving that change because the normalSave As
won't save that information.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
.要跟进 SLAks 答案:要使用图像的像素尺寸(无论 DPI 如何),您可以将宽度和高度绑定到源的 PixelWidth 和 PixelHeight,如下所示
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