System.Drawing.Icon and hIcon (ptr to an ICONIMAGE structure)

W

WALDO

I have an interesting quandary.

I have developed a program in VB.Net to extract an icon resource from an
exe/dll or from an ico file and enumerate its formats (16x16,256
color;32x32,true color;etc.). It reads out the structures all the way down
to the ICONIMAGE structures. I have this represented as an array of bytes
read from the resource. I am using the unmanaged function
CreateIconFromResourceEx to generate an hIcon pointer. I take this icon
pointer and pass it into the FromHandle method of a System.Drawing.Icon.

Everything works beautifully with one exception. When the icon format is
monochrome, the height from the System.Drawing.Icon is doubled. I understand
that the biHeight from the BITMAPINFOHEADER structure is doubled to account
for both the XOR (color) and AND (transparent) masks, but I am passing the
correct desired height (biHeight / 2) into CreateIconFromResourceEx.

Is there something wrong with the pointer I'm getting back, or is there
something wrong with System.Drawing.Icon that it doesn't understand
monochrome icons?

[VB.Net Pseudocode]
Public Structure ICONIMAGE
Public pBits() As Byte 'All the bytes that make up the structure from
the resource.
Public Function GetHeader As BITMAPINFOHEADER
....
End Function
End Structure


....
Dim img as ICONIMAGE
....

Dim bmi As BITMAPINFOHEADER = img.GetHeader()
Dim width As Integer = bmi.biWidth
Dim height As Integer = bmi.biHeight \ 2
Dim flags As Integer = 0
If IsMonoChrome() Then
flags = flags Or LR_MONOCHROME
End If

Dim hIcon As IntPtr
hIcon = CreateIconFromResourceEx(img.lpBits, ico.idEntries(i).dwBytesInRes,
True, &H30000, width, height, flags)

Dim icon As System.Drawing.Icon
icon = icon.FromHandle(hIcon)

Debug.WriteLine(i, "i")
Debug.WriteLine(icon.Size, "Size")
Debug.WriteLine("")

....
 
M

Mike D Sutton

I have an interesting quandary.
I have developed a program in VB.Net to extract an icon resource from an
exe/dll or from an ico file and enumerate its formats (16x16,256
color;32x32,true color;etc.). It reads out the structures all the way down
to the ICONIMAGE structures. I have this represented as an array of bytes
read from the resource. I am using the unmanaged function
CreateIconFromResourceEx to generate an hIcon pointer. I take this icon
pointer and pass it into the FromHandle method of a System.Drawing.Icon.

Everything works beautifully with one exception. When the icon format is
monochrome, the height from the System.Drawing.Icon is doubled. I understand
that the biHeight from the BITMAPINFOHEADER structure is doubled to account
for both the XOR (color) and AND (transparent) masks, but I am passing the
correct desired height (biHeight / 2) into CreateIconFromResourceEx.

Is there something wrong with the pointer I'm getting back, or is there
something wrong with System.Drawing.Icon that it doesn't understand
monochrome icons?

As has been mentioned before, VB.NET queries should really go to a more appropriate group such as those listed here:
Http://EDais.mvps.org/DotNet/
In answer to your question though, monochrome icons are stored using a single 1-Bit Bitmap rather then two as with other
bit-depths. I don't recall offhand which way round the images are within this Bitmap but this should be a simple test
in your code - If you can't work it out then let me know and I'll have a look at some of my old code to extract them.
The height of that particular ICONDIRENTRY will be doubled and as such should be halved to get the real size of the icon
and the data dealt with accordingly.
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: (e-mail address removed)
WWW: Http://EDais.mvps.org/
 
W

WALDO

Right, I remember reading that somewhere. So now my question is
How do I split up my byte array (ICONIMAGE) to accommodate that?

Do I have to create another ICONIMAGE structure because of that when it's
monochrome? If so, how do I split up the AND mask into its constituent XOR
and AND masks?

Mike D Sutton said:
As has been mentioned before, VB.NET queries should really go to a more
appropriate group such as those listed here:
Http://EDais.mvps.org/DotNet/
In answer to your question though, monochrome icons are stored using a
single 1-Bit Bitmap rather then two as with other
bit-depths. I don't recall offhand which way round the images are within
this Bitmap but this should be a simple test
in your code - If you can't work it out then let me know and I'll have a
look at some of my old code to extract them.
The height of that particular ICONDIRENTRY will be doubled and as such
should be halved to get the real size of the icon
 
M

Mike D Sutton

Right, I remember reading that somewhere. So now my question is
How do I split up my byte array (ICONIMAGE) to accommodate that?

Do I have to create another ICONIMAGE structure because of that when it's
monochrome? If so, how do I split up the AND mask into its constituent XOR
and AND masks?

Does CreateIconFromResourceEx() not handle them as they are?
If not then you'll have to create the hIcon yourself using CreateIconIndirect() as I mentioned in a prior post of
yours - Just create a monochrome Bitmap from the data, then use this as the Mask member of the ICONINFO structure you
pass to the function.
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: (e-mail address removed)
WWW: Http://EDais.mvps.org/
 
W

WALDO

CreateIconFromResourceEx gives me an hIcon, but for the monochrome guys, it
gives me double the height. I tried passing in the correct height into
CreateIconFromResourceEx, but it still gave me double the height. Then I
tried passing half the height to CreateIconFromResourceEx on known
monochrome icons. The height came out right, but the scanning didn't.

Perhaps you could provide a code sample of dealing with monochrome icons.

Mike D Sutton said:
Does CreateIconFromResourceEx() not handle them as they are?
If not then you'll have to create the hIcon yourself using
CreateIconIndirect() as I mentioned in a prior post of
yours - Just create a monochrome Bitmap from the data, then use this as
the Mask member of the ICONINFO structure you
 
M

Mike D Sutton

CreateIconFromResourceEx gives me an hIcon, but for the monochrome guys, it
gives me double the height. I tried passing in the correct height into
CreateIconFromResourceEx, but it still gave me double the height. Then I
tried passing half the height to CreateIconFromResourceEx on known
monochrome icons. The height came out right, but the scanning didn't.

Perhaps you could provide a code sample of dealing with monochrome icons.

Not for VB.NET unfortunately, for that you'd need to post to another group I'm afraid, and I don't know of any examples
offhand written in VB either. The call you need to look at though is as mentioned before, CreateIconIndirect() - Simply
create a single monochrome Bitmap (either DDB or DIB) and populate it with the data array from the ICONIMAGE structure
then send this as the mask bitmap to the afore mentioned API call, it will return an hIcon perfectly compatible with
those you're currently using.
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: (e-mail address removed)
WWW: Http://EDais.mvps.org/
 
W

WALDO

I'll take C# if you got it. I'll even live with C++.
This wasn't specifically a VB question, but one of GDI/GDI+ (unmanaged vs.
managed). I just happened to write it in VB.Net
Any code sample will be appreciated.

Mike D Sutton said:
icons.

Not for VB.NET unfortunately, for that you'd need to post to another group
I'm afraid, and I don't know of any examples
offhand written in VB either. The call you need to look at though is as
mentioned before, CreateIconIndirect() - Simply
create a single monochrome Bitmap (either DDB or DIB) and populate it with
the data array from the ICONIMAGE structure
then send this as the mask bitmap to the afore mentioned API call, it will
return an hIcon perfectly compatible with
 
M

Mike D Sutton

I'll take C# if you got it. I'll even live with C++.
This wasn't specifically a VB question, but one of GDI/GDI+ (unmanaged vs.
managed). I just happened to write it in VB.Net
Any code sample will be appreciated.

I've given you all the information you need (it's not a complex task!), have a hack at implementing it yourself and I'm
sure Google can fill in any gaps you have associated with any of the API's.
Hope this helps,

Mike


- Microsoft Visual Basic MVP -
E-Mail: (e-mail address removed)
WWW: Http://EDais.mvps.org/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top