Icon as a constant

Is it possible to convert an icon/image to a string or array literal and obtain a handle to it that a Window/Window-class can use?
The best I can offer since I don't know how much memory is used by HBITMAP, is to base64-encode the picture file and then add the base64 string as a string literal. When you need the image, you revert the encoding and use LoadImage().
Why bother to encode/decode in base64 ? You could use a regular char array to store image content. Of course, write the array as a sequence of hex numbers.
Why do you want this anyway and not use a resource file ?
Is your program portable outside of windows world or you just not want people extract your images with a resource editor ?
It's more just to test to see if it can be done. 'Writing the array as a series of hex numbers' doesn't give me any insight as to how to convert it to a Windows handle. I also can't peek the data inside a handle, or I don't know how to. LoadIcon and LoadImage don't appear to accept a pointer to the data, but maybe there's a way to trick it into doing so?
To encode the memory representation of the file is a different animal. Window handles are just "meaningless" pointers only understood by MS code. In other words: Microsoft reserves the knowledge of how the data is stored and indexed in memory based on the value of the handle. This is why I told you to encode the file itself not its memory representation.

Having said that, I guess a bitmap CAN be accessed. Read about GetDIBits() and similar functions, and see if you can get a complete image of the bitmap in memory. But then again, why all that trouble when you can easily serialize the file itself?
Well, if you have the right kind of array of bytes, you can feed it to CreateIcon() or CreateIconIndirect(). Then you'd know what data Windows started off with (though not what it did with it).

Or you can use GetIconInfo() to retrieve the HBITMAPs associated with an icon, then feed these to GetObject() to retrieve the bitmap data.

See MSDN for details.

Andy
Last edited on
Topic archived. No new replies allowed.