473,395 Members | 1,975 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 7436
> 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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.