在 WPF/C# 中调整和使用 ICO 文件
在我正在开发的应用程序中,图像(System.Windows.Media.Imaging
的一部分)在 XAML 页面的代码隐藏中动态创建,并添加到窗口中。我正在使用 ICO 文件,其中包含各种大小和位深度。我想要的大小是 96x96 @ 32 位。代码自动选择最大尺寸(256x256 @ 32 位)。
我将 Image.Source
属性设置如下:
image.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/Images/Application.ico"));
现在,通过搜索,我找到了 Icon(System.Drawing
的一部分),它允许我设置字符串路径和大小,但这是完全不同的库的一部分。
new Icon("pack://application:,,,/Resources/Images/Inquiry.ico",96,96);
知道如何使图像的大小属性更易于管理吗?谢谢你!
编辑: 将 System.Drawing.Icon 转换为 System.Media.ImageSource
这篇文章使图像看起来更小,但是,如果我将图标设置为 96x96 或 128x128 并不重要,它实际上不会改变显示图像的大小。我假设它的默认值与 System.Windows.Media.Imaging 默认值不同。
编辑到编辑:奇怪变得越来越奇怪。当我通过原始方法显示时,它肯定将图像显示为256x256。但是,当我使用链接中描述的转换方法时,虽然尺寸发生变化,但它们显着缩小(即256x256看起来更接近48x48)。
In an application I'm working on there are Images (part of System.Windows.Media.Imaging
) being created dynamically in the code-behind of a XAML page and getting added to the window. I'm using ICO files, which contain various sizes and bit depths. The size I want is 96x96 @ 32 bit. The code automatically selects the largest size (256x256 @ 32 bit).
I'm setting the Image.Source
property as follows:
image.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/Images/Application.ico"));
Now, by searching around I've found the Icon (part of System.Drawing
) which will allow me to set a string path and size, but this is a part of an entirely different library.
new Icon("pack://application:,,,/Resources/Images/Inquiry.ico",96,96);
Any idea how I can make my Image's size property a bit more manageable? Thank you!
EDIT:
Convert System.Drawing.Icon to System.Media.ImageSource
This post gets the image to appear smaller, however, it doesn't matter if I set the Icon to 96x96 or 128x128, it doesn't actually change the displayed image's size. I'm assuming it's going to some default value that is different that the System.Windows.Media.Imaging
default.
EDIT to the EDIT: Weird keeps getting weirder. When I display through the original method, it's definitely displaying the image as 256x256. However, when I use the conversion method described in the link, while the sizes change, they are significantly scaled down (i.e. 256x256 looks closer to 48x48).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试另一个 SO 问题的答案中发布的标记扩展。请注意使用
BitmapDecoder
从 Ico 文件中获取所需的帧。Try the markup extension that is posted in this answer to another SO question. Note the use of
BitmapDecoder
to get the required frame from the Ico file.您可以尝试使用以下代码,它应该适用于 ICO 文件:
You could try with the following code, it should work for ICO files:
我还没有尝试过使用 ico 文件,我认为这里可能有用。
I havent tried with ico files, I think could be useful here.