Monday, December 28, 2009

Icons in .NET. Why is so difficult?

Once more I find myself needing to use icons in .NET.

A quick search in Google shows a plethora of questions and workarounds to use icons in .NET. From simple calls to Win32 API to a whole library written from scratch.

Let's say you have the following code:
Dim ico As New Icon("file.ico", 16, 16)
That would load an 16x16 icon from the specified "file.ico", and if you use this icon, chances are that it will be OK for the most applications.

And even if you have
Dim ico As New Icon("file.ico", 32, 32)
it will pretty much be acceptable for almost any situation.

Now when you need to use the (not so) new Vista icons... Oh boy! you're in for a ride! Because
Dim ico As New Icon("file.ico", 256, 256)
Gives you a 32x32 icon! And not a 256x256 icon as per documentation.

What the heck is going on here?

With nothing better to do right now I decided to see if I could find why exactly .NET framework doesn't handle well icons when you need to load them with different sizes.

Having my faithful Reflector handy I dove down the Icon class constructor:
public Icon(string fileName, int width, int height) : this()
{
using (FileStream stream = new
FileStream(fileName,
FileMode.Open,
FileAccess.Read,
FileShare.Read))
{
this.iconData = new byte[(int) stream.Length];
stream.Read(this.iconData, 0, this.iconData.Length);
}
this.Initialize(width, height);
}
Here the framework simply loads the whole file in memory and calls an initialization function, where the magic happens.
private unsafe void Initialize(int width, int height)
{
if ((this.iconData == null) || (this.handle != IntPtr.Zero))
throw new InvalidOperationException
(SR.GetString("IllegalState",
new object[] { base.GetType().Name }));

if (this.iconData.Length <
Marshal.SizeOf(typeof(SafeNativeMethods.ICONDIR)))
throw new ArgumentException(SR.GetString
("InvalidPictureType",
new object[] { "picture", "Icon" }));

if (width == 0)
width = UnsafeNativeMethods.GetSystemMetrics(11);

if (height == 0)
height = UnsafeNativeMethods.GetSystemMetrics(12);

if (bitDepth == 0)
{
IntPtr dC = UnsafeNativeMethods.
GetDC(NativeMethods.NullHandleRef);
bitDepth = UnsafeNativeMethods.
GetDeviceCaps(new HandleRef(null, dC), 12);
bitDepth *= UnsafeNativeMethods.
GetDeviceCaps(new HandleRef(null, dC), 14);
UnsafeNativeMethods.
ReleaseDC(NativeMethods.NullHandleRef,
new HandleRef(null, dC));
if (bitDepth == 8)
bitDepth = 4;
}
fixed (byte* numRef = this.iconData)
{
short @short = this.GetShort(numRef);
short num2 = this.GetShort(numRef + 2);
short num3 = this.GetShort(numRef + 4);
if (((@short != 0) || (num2 != 1)) || (num3 == 0))
throw new ArgumentException(SR.GetString
("InvalidPictureType",
new object[] { "picture", "Icon" }));

byte bWidth = 0;
byte bHeight = 0;
int length = 0;
byte* numPtr = numRef + 6;
int num7 = Marshal.
SizeOf(typeof(SafeNativeMethods.ICONDIRENTRY));
if ((num7 * num3) >= this.iconData.Length)
throw new ArgumentException(SR.GetString
("InvalidPictureType",
new object[] { "picture", "Icon" }));

for (int i = 0; i < num3; i++)
{
SafeNativeMethods.ICONDIRENTRY entry;
entry.bWidth = numPtr[0];
entry.bHeight = numPtr[1];
entry.bColorCount = numPtr[2];
entry.bReserved = numPtr[3];
entry.wPlanes = this.GetShort(numPtr + 4);
entry.wBitCount = this.GetShort(numPtr + 6);
entry.dwBytesInRes = this.GetInt(numPtr + 8);
entry.dwImageOffset = this.GetInt(numPtr + 12);
bool flag = false;
int wBitCount = 0;
if (entry.bColorCount != 0)
{
wBitCount = 4;
if (entry.bColorCount < 0x10)
wBitCount = 1;

}
else
wBitCount = entry.wBitCount;

if (wBitCount == 0)
wBitCount = 8;

if (length == 0)
flag = true;

else
{
int num10 = Math.Abs((int)(bWidth - width)) +
Math.Abs((int)(bHeight - height));
int num11 = Math.Abs((int)(entry.bWidth - width)) +
Math.Abs((int)(entry.bHeight - height));
if ((num11 < num10) || ((num11 == num10) &&
(((wBitCount <= bitDepth) &&
(wBitCount > this.bestBitDepth)) ||
((bitDepth < this.bestBitDepth) &&
(wBitCount < this.bestBitDepth)))))
flag = true;
}
if (flag)
{
bWidth = entry.bWidth;
bHeight = entry.bHeight;
this.bestImageOffset = entry.dwImageOffset;
length = entry.dwBytesInRes;
this.bestBitDepth = wBitCount;
}
numPtr += num7;
}
if ((this.bestImageOffset < 0) ||
((this.bestImageOffset + length) > this.iconData.Length))
throw new ArgumentException(SR.GetString
("InvalidPictureType",
new object[] { "picture", "Icon" }));

if ((this.bestImageOffset % IntPtr.Size) != 0)
{
byte[] destinationArray = new byte[length];
Array.Copy(this.iconData,
this.bestImageOffset,
destinationArray, 0, length);
fixed (byte* numRef2 = destinationArray)
{
this.handle = SafeNativeMethods.
CreateIconFromResourceEx(numRef2, length,
true, 0x30000, 0, 0, 0);
}
}
else
this.handle = SafeNativeMethods.
CreateIconFromResourceEx(numRef + this.bestImageOffset,
length, true, 0x30000, 0, 0, 0);

if (this.handle == IntPtr.Zero)
throw new Win32Exception();
}
}
Grazing over the above code, it simply reads the array of bytes that the file was transformed into, following the icon file format specification reading through every icon format trying to match the width and size requested (or the best next thing).

And then at the very end it calls CreateIconFromResourceEx to create the icon handle... And BANG! There goes the carefully found measured icon down the drain.

Now, take a look at the call of the function:
this.handle = SafeNativeMethods.
CreateIconFromResourceEx(numRef2, length,
true, 0x30000, 0, 0, 0);

// OR

this.handle = SafeNativeMethods.
CreateIconFromResourceEx(numRef + this.bestImageOffset, length,
true, 0x30000, 0, 0, 0);
You see, CreateIconFromResourceEx is simply a proxy call to the native function in the user32.dll, and according to the documentation the function recieves the following parameters:

pbIconBits
[in] Pointer to a buffer containing the icon or cursor resource bits. These bits are typically loaded by calls to the LookupIconIdFromDirectoryEx and LoadResource functions.

cbIconBits
[in] Specifies the size, in bytes, of the set of bits pointed to by the pbIconBits parameter.

fIcon
[in] Specifies whether an icon or a cursor is to be created. If this parameter is TRUE, an icon is to be created. If it is FALSE, a cursor is to be created.

dwVersion

[in] Specifies the version number of the icon or cursor format for the resource bits pointed to by the pbIconBits parameter. This parameter can be 0x00030000.

cxDesired
[in] Specifies the desired width, in pixels, of the icon or cursor. If this parameter is zero, the function uses the SM_CXICON or SM_CXCURSOR system metric value to set the width.

cyDesired
[in] Specifies the desired height, in pixels, of the icon or cursor. If this parameter is zero, the function uses the SM_CYICON or SM_CYCURSOR system metric value to set the height.

uFlags
[in] Specifies a combination of the following values:
LR_DEFAULTCOLOR
Uses the default color format.

LR_DEFAULTSIZE
Uses the width or height specified by the system metric values for cursors or icons, if the cxDesired or cyDesired values are set to zero. If this flag is not specified and cxDesired and cyDesired are set to zero, the function uses the actual resource size. If the resource contains multiple images, the function uses the size of the first image.

LR_MONOCHROME
Creates a monochrome icon or cursor.

LR_SHARED
Shares the icon or cursor handle if the icon or cursor is created multiple times. If LR_SHARED is not set, a second call to CreateIconFromResourceEx for the same resource will create the icon or cursor again and return a different handle.
When you use this flag, the system will destroy the resource when it is no longer needed.
Do not use LR_SHARED for icons or cursors that have non-standard sizes, that may change after loading, or that are loaded from a file.
When loading a system icon or cursor, you must use LR_SHARED or the function will fail to load the resource.
Windows 95/98/Me: The function finds the first image with the requested resource name in the cache, regardless of the size requested.

If we check the parameters passed by the Initialize method we'll see that the cxDesired and cyDesired parameters are ZERO.

And once more, according to the documentation, if any of these parameters are ZERO, the API will call GetSystemMetrics in order to discover the values of SM_CXICON and SM_CYICON, respectively, in which case the Windows happily answer as (usually) 32.

And if the guys at Redmond manage to change the above lines to:
this.handle = SafeNativeMethods.
CreateIconFromResourceEx(numRef2, length,
true, 0x30000, width, height, 0);

// AND

this.handle = SafeNativeMethods.
CreateIconFromResourceEx(numRef + this.bestImageOffset, length,
true, 0x30000, width, height, 0);
They will fix the dam problem and we will not need to resort to workarounds (and the like) anymore.

Edited to Add:
One other thing I noted when I was testing this is that the width and height information stored in the .ico file are ZERO if the icon is a PNG image.

With this information, and the code above, I attempt to circumvent the inability of .NET to work with Vista Icons by using
Dim ico = New Icon("vistaIcon.ico", 1, 1)
And, as expected the icon was loaded correctly and reported a 256x256 size.

However, when I attempted to call ico.ToBitmap() to acquire an usable Image, I got the following exception:
ArgumentOutOfRangeException: 
"Requested range extends past the end of the array."

StackTrace:
at System.Runtime.InteropServices.Marshal.CopyToNative(...)
at System.Runtime.InteropServices.Marshal.Copy(...)
at System.Drawing.Icon.ToBitmap()
I didn't dove into the ToBitmap() function yet, but the fact that the icon reported 256x256 is promissing.

I've developed a workaround class that can be used as a replacement for the Icon class. You can download it here.

0 Comments:

Post a Comment