【问题标题】:How to retrieve the largest available image from an icon file, using C#如何使用 C# 从图标文件中检索最大的可用图像
【发布时间】:2016-01-29 09:41:29
【问题描述】:

我需要从包含多种图像尺寸的图标文件 (.ico) 中检索 最大 位图图像。

在我的图标编辑器中,我可以看到图标文件中有几个图像,分别是 16x16px、24x24、32x32、48x48 和 256x256。

但是,以下代码行错过了我想要的 256x256 图像:

var iconObj = new System.Drawing.Icon(TempFilename); //iconObj is 32x32
//get the 'default' size image:
var image1 = iconObj.ToBitmap(); //retrieved image is 32x32
//get the largest image up to 1024x1024:
var image2 = new System.Drawing.Icon(iconObj, new System.Drawing.Size(1024, 1024)).ToBitmap(); //retrieved image is 48x48

如何从图标文件中获得最大的可用图像(或特定尺寸)?

【问题讨论】:

标签: c# .net system.drawing


【解决方案1】:

看起来,微软在他们的实施中有错误,没有接受 考虑图标格式“以像素为单位指定图像宽度。可以是 0 到 255 之间的任意数字。值 0 表示图像宽度为 256 像素”。因此,从 Icon(string, size) 返回的图标的最大大小 可以是 128x128。 我找到了这个解决方法:当我将 -1,-1 指定为高度和宽度时,结果将是 256x256 图标(不确定 ico 文件中图像的任何顺序,未提供按尺寸排序的规范,我只是使用它在我的项目中)

using System;
using System.Drawing;

static class Program
{

    static void Main()
    {
        Icon image2 = new System.Drawing.Icon("Softskin-Series-Folder-Folder-BD-Files.ico",-1,-1);
        Console.WriteLine( "height={0} width={1}",image2.Height,image2.Width    ); 

    }
}

【讨论】:

  • 是的,似乎是这样。如果我将目标高度和宽度减小到 128 或更少,它会按预期工作。
猜你喜欢
  • 2014-11-08
  • 2017-07-18
  • 2016-10-17
  • 1970-01-01
  • 2012-02-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多