Happy New Year!!!
![](http://journeyhomeburke.files.wordpress.com/2008/01/happy-new-year-fireworks.jpg)
Thoughts and babbling about software development. Visual Basic, .NET platform, web, etc.
Thursday, December 31, 2009
Wednesday, December 30, 2009
Monday, December 28, 2009
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.Dim ico As New Icon("file.ico", 32, 32)it will pretty much be acceptable for almost any situation.
Dim ico As New Icon("file.ico", 256, 256)Gives you a 32x32 icon! And not a 256x256 icon as per documentation.
public Icon(string fileName, int width, int height) : this()Here the framework simply loads the whole file in memory and calls an initialization function, where the magic happens.
{
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);
}
private unsafe void Initialize(int width, int height)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).
{
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();
}
}
CreateIconFromResourceEx
to create the icon handle... And BANG! There goes the carefully found measured icon down the drain.this.handle = SafeNativeMethods.You see,
CreateIconFromResourceEx(numRef2, length,
true, 0x30000, 0, 0, 0);
// OR
this.handle = SafeNativeMethods.
CreateIconFromResourceEx(numRef + this.bestImageOffset, length,
true, 0x30000, 0, 0, 0);
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.
Initialize
method we'll see that the cxDesired and cyDesired parameters are ZERO.SM_CXICON
and SM_CYICON
, respectively, in which case the Windows happily answer as (usually) 32.this.handle = SafeNativeMethods.They will fix the dam problem and we will not need to resort to workarounds (and the like) anymore.
CreateIconFromResourceEx(numRef2, length,
true, 0x30000, width, height, 0);
// AND
this.handle = SafeNativeMethods.
CreateIconFromResourceEx(numRef + this.bestImageOffset, length,
true, 0x30000, width, height, 0);
.ico
file are ZERO if the icon is a PNG image.Dim ico = New Icon("vistaIcon.ico", 1, 1)And, as expected the icon was loaded correctly and reported a 256x256 size.
ArgumentOutOfRangeException:I didn't dove into the ToBitmap() function yet, but the fact that the icon reported 256x256 is promissing.
"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()
Friday, December 25, 2009
Wednesday, December 23, 2009