Reputation: 4850
I need to retrieve the largest bitmap image from an icon file (.ico) that contains several sizes of the image.
In my icon editor I can see that there are several images in the icon file, namely 16x16px, 24x24, 32x32, 48x48 and 256x256.
However the following lines of code miss the 256x256 image which is the one I want:
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
How do I get the largest image available (or a specific size) from an icon file?
Upvotes: 3
Views: 1365
Reputation: 735
Looks, like Microsoft has bug in their implementation, not takes in consideration icon format "Specifies image width in pixels. Can be any number between 0 and 255. Value 0 means image width is 256 pixels". So, maximum size of returned icon from Icon(string, size) can be 128x128. I found this workaround: when i specify -1,-1 as height and width, the result will be 256x256 icon (am not sure for any order of images in ico file, by specification ordering by size is not provided, i just use it in my project)
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 );
}
}
Upvotes: 4