调整和使用WPF/C中的ICO文件#

Wal*_*ung 10 c# size wpf icons image

在我正在处理的应用程序中,有一些图像(部分System.Windows.Media.Imaging)是在XAML页面的代码隐藏中动态创建的,并且被添加到窗口中.我正在使用包含各种大小和位深度的ICO文件.我想要的大小是96x96 @ 32位.代码自动选择最大的大小(256x256 @ 32位).

我正在设置Image.Source属性如下:

image.Source = new BitmapImage(new Uri("pack://application:,,,/Resources/Images/Application.ico"));
Run Code Online (Sandbox Code Playgroud)

现在,通过搜索我发现了Icon(部分System.Drawing),它将允许我设置字符串路径和大小,但这是一个完全不同的库的一部分.

new Icon("pack://application:,,,/Resources/Images/Inquiry.ico",96,96);
Run Code Online (Sandbox Code Playgroud)

知道如何让我的Image尺寸属性更易于管理吗?谢谢!

编辑: 将System.Drawing.Icon转换为System.Media.ImageSource
这篇文章让图像看起来更小,但是,如果我将Icon设置为96x96或128x128并不重要,它实际上并没有改变显示图像的大小.我假设它的默认值与默认值不同System.Windows.Media.Imaging.

编辑到编辑:怪异不断变得更奇怪.当我通过原始方法显示时,它肯定会将图像显示为256x256.但是,当我使用链接中描述的转换方法时,虽然大小发生变化,但它们会大幅缩小(即256x256看起来更接近48x48).

Nem*_*nda 0

您可以尝试使用以下代码,它应该适用于 ICO 文件:

Image displayImage = new Image();
// Create the source
BitmapImage sourceImage = new BitmapImage();
sourceImage.BeginInit();
sourceImage.UriSource = new Uri("pack://application:,,,/Resources/Images/Application.ico");
sourceImage.EndInit();
// Set the source
displayImage.Source = sourceImage;
// Set the size you want
displayImage.Width = 96;
displayImage.Stretch = Stretch.Uniform;
Run Code Online (Sandbox Code Playgroud)