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

发布于 2024-12-08 18:43:07 字数 988 浏览 1 评论 0原文

在我正在开发的应用程序中,图像(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 技术交流群。

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

发布评论

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

评论(3

故事灯 2024-12-15 18:43:07

尝试另一个 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.

土豪我们做朋友吧 2024-12-15 18:43:07

您可以尝试使用以下代码,它应该适用于 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;

You could try with the following code, it should work for ICO files:

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;
日裸衫吸 2024-12-15 18:43:07

我还没有尝试过使用 ico 文件,我认为这里可能有用。

    /// <summary>
    /// Resizes image with high quality
    /// </summary>
    /// <param name="imgToResize">image to be resized</param>
    /// <param name="size">new size</param>
    /// <returns>new resized image</returns>
    public Image GetResizedImage(Image imgToResize, Size size) 
    {
      try
      {
       if (imgToResize != null && size != null)
       {
         if (imgToResize.Height == size.Height && imgToResize.Width == size.Width)
         {
            Image newImage = (Image)imgToResize.Clone();
            imgToResize.Dispose();
            return newImage;
         }
         else
         {
            Image newImage = (Image)imgToResize.Clone();
            imgToResize.Dispose();
            Bitmap b = new Bitmap(size.Width, size.Height);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(newImage, 0, 0, size.Width, size.Height);
            g.Dispose();
            return (Image)b;
        }
      }
       return null;
     }
     catch (Exception e)
     {
       log.Error("Exception in Resizing an image ", e);
       return null;
     }
    }

I havent tried with ico files, I think could be useful here.

    /// <summary>
    /// Resizes image with high quality
    /// </summary>
    /// <param name="imgToResize">image to be resized</param>
    /// <param name="size">new size</param>
    /// <returns>new resized image</returns>
    public Image GetResizedImage(Image imgToResize, Size size) 
    {
      try
      {
       if (imgToResize != null && size != null)
       {
         if (imgToResize.Height == size.Height && imgToResize.Width == size.Width)
         {
            Image newImage = (Image)imgToResize.Clone();
            imgToResize.Dispose();
            return newImage;
         }
         else
         {
            Image newImage = (Image)imgToResize.Clone();
            imgToResize.Dispose();
            Bitmap b = new Bitmap(size.Width, size.Height);
            Graphics g = Graphics.FromImage((Image)b);
            g.InterpolationMode = InterpolationMode.HighQualityBicubic;
            g.DrawImage(newImage, 0, 0, size.Width, size.Height);
            g.Dispose();
            return (Image)b;
        }
      }
       return null;
     }
     catch (Exception e)
     {
       log.Error("Exception in Resizing an image ", e);
       return null;
     }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文