471,338 Members | 1,046 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,338 software developers and data experts.

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

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("")

....
Jul 21 '05 #1
7 7096
> 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: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Jul 21 '05 #2
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" <ED***@mvps.org> wrote in message
news:OF**************@TK2MSFTNGP10.phx.gbl...
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: ED***@mvps.org
WWW: Http://EDais.mvps.org/

Jul 21 '05 #3
> 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: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Jul 21 '05 #4
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" <ED***@mvps.org> wrote in message
news:%2***************@TK2MSFTNGP09.phx.gbl...
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: ED***@mvps.org
WWW: Http://EDais.mvps.org/

Jul 21 '05 #5
> 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: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Jul 21 '05 #6
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" <ED***@mvps.org> wrote in message
news:Om**************@TK2MSFTNGP10.phx.gbl...
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: ED***@mvps.org
WWW: Http://EDais.mvps.org/

Jul 21 '05 #7
> 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: ED***@mvps.org
WWW: Http://EDais.mvps.org/
Jul 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by yxq | last post: by
5 posts views Thread by BJ | last post: by
4 posts views Thread by Urs Vogel | last post: by
4 posts views Thread by Neo | last post: by
reply views Thread by =?Utf-8?B?SnVzdCBjbG9zZSB5b3VyIGV5ZXMgYW5kIHNlZQ== | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.