472,958 Members | 2,366 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 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 7337
> 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: yxq | last post by:
Thank Armin Zingler, the codes below work well! But i want to put the icon into Picturebox1 instead of the Form1, how to do? the code ************************** If retval <> 0 Then Dim g As...
5
by: BJ | last post by:
I am trying to add my application to the system tray. I am not using the built in control becuase of the limitation that you can not use the balloon tool tip feature. I have done this succesfully...
4
by: Urs Vogel | last post by:
Hi I have an image list with icons and would like to change the form icon depending on an operating mode, but I couldn't figure out how to re-assign an image from the image list to the form.icon...
7
by: WALDO | last post by:
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...
4
by: Neo | last post by:
I am developing a GUI for a destop application in Visual C++ .Net 2005, I am trying to create toolbar exactlly same as windows explorer which will have default windows icons, which are in windows...
0
by: Nickneem | last post by:
I' m trying to disable all right mouse clicks by using the vbAccelerator Windows Hooks Library The small (systray / console) app. must catch all (right) mouseclicks before they are received by...
1
by: GHUM | last post by:
I have found a "make a icon in traybar" skript, and it loads its Icon from a file hinst = win32gui.GetModuleHandle(None) iconPathName= "c:/myapp/myapp.ico" icon_flags =...
0
by: =?Utf-8?B?SnVzdCBjbG9zZSB5b3VyIGV5ZXMgYW5kIHNlZQ== | last post by:
Hello all, I have SDI Application had made using MFC Application wizard, and I could able to change the icon of the application executable file through add adding a resource icon and replace it...
2
by: Nathan Sokalski | last post by:
I am attempting to create icons for controls I have created using VB.NET by using the System.Drawing.ToolboxBitmap attribute. I have managed to do this in C# by specifying the path to the *.ico...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.