如何使用C#从图标文件中检索最大的可用图像

时间:2016-01-29 09:41:30

标签: c# .net system.drawing

我需要从包含多种尺寸图像的图标文件(.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

如何从图标文件中获取最大可用图像(或特定大小)?

1 个答案:

答案 0 :(得分:2)

看起来,就像微软在实施方面存在漏洞一样,没有接受 考虑图标格式"指定图像宽度(以像素为单位)。可 0到255之间的任何数字。值0表示图像宽度为256 像素&#34 ;.因此,Icon的返回图标的最大大小(字符串,大小) 可以是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    ); 

    }
}
相关问题