How to serialize an ICON
-
I would like to "serialize" an ICON, i.e. express it as a simple sequence of bytes that could be written/read from a database. A .ico file could be treated as the serialization of an ICON, but that would probably be wasteful, as a .ico file can store icon images for a variety of icon sizes, and I am just interested in one device (32x32 as it happens). An ICONINFO structure requires two BITMAPs, one for the mask and one for the color bits.. which is a bit messy. Is there a simple way of serializing one ICON into ONE blob ?
cheers, Neil
-
I would like to "serialize" an ICON, i.e. express it as a simple sequence of bytes that could be written/read from a database. A .ico file could be treated as the serialization of an ICON, but that would probably be wasteful, as a .ico file can store icon images for a variety of icon sizes, and I am just interested in one device (32x32 as it happens). An ICONINFO structure requires two BITMAPs, one for the mask and one for the color bits.. which is a bit messy. Is there a simple way of serializing one ICON into ONE blob ?
cheers, Neil
Why not just save the color bitmap and create a mask at runtime? You could go further since all your icons will be the same size, save only the array of bits from the color bitmap, and add the bitmap headers at runtime.
-
I would like to "serialize" an ICON, i.e. express it as a simple sequence of bytes that could be written/read from a database. A .ico file could be treated as the serialization of an ICON, but that would probably be wasteful, as a .ico file can store icon images for a variety of icon sizes, and I am just interested in one device (32x32 as it happens). An ICONINFO structure requires two BITMAPs, one for the mask and one for the color bits.. which is a bit messy. Is there a simple way of serializing one ICON into ONE blob ?
cheers, Neil
Try to use FindResource, LoadResource, SizeofResource (size in bytes) and LockResource (get the pointer to the first byte). Probably you'll have to work with #pragma pack(1), but I'm not sure. Anyway, SizeofResource+LockResource will get you a pointer to a byte array and its size; you can simply get the byte array and store in a blob (perhaps also size should be stored as well).
HRSRC hrcIcon = FindResource(hExeOrDll, MAKEINTRESOURCE(IDI_YOURICON), RT_ICON);
HGLOBAL hgl = LoadResource(hExeOrDll, hrcIcon);
LPVOID pvIcon = LockResource(hgl);
DWORD dwSizeInBytes = SizeofResource(hExeOrDll, hrcIcon);
#pragma pack(push, 1)
BYTE *pbIcon = (BYTE *)malloc(dwSizeInBytes * sizeof(BYTE));
CopyMemory((PVOID)pbIcon, pvIcon, dwSizeInBytes);
#pragma pack(pop)
// save byte array pbIcon of size dwSizeInBytes
free(pbIcon);Nuclear launch detected
-
Why not just save the color bitmap and create a mask at runtime? You could go further since all your icons will be the same size, save only the array of bits from the color bitmap, and add the bitmap headers at runtime.
That's a good idea. How do I get at the BITMAP stucture, starting from an HICON ? I saw GetIconInfo(), GetBitmapBits(), but the latter function doesn't seem to tell you how much memory to allocate - so I can't see how I can use it ! Presumably it is simple to create the mask BITMAP from the color BITMAP?
cheers, Neil
-
Try to use FindResource, LoadResource, SizeofResource (size in bytes) and LockResource (get the pointer to the first byte). Probably you'll have to work with #pragma pack(1), but I'm not sure. Anyway, SizeofResource+LockResource will get you a pointer to a byte array and its size; you can simply get the byte array and store in a blob (perhaps also size should be stored as well).
HRSRC hrcIcon = FindResource(hExeOrDll, MAKEINTRESOURCE(IDI_YOURICON), RT_ICON);
HGLOBAL hgl = LoadResource(hExeOrDll, hrcIcon);
LPVOID pvIcon = LockResource(hgl);
DWORD dwSizeInBytes = SizeofResource(hExeOrDll, hrcIcon);
#pragma pack(push, 1)
BYTE *pbIcon = (BYTE *)malloc(dwSizeInBytes * sizeof(BYTE));
CopyMemory((PVOID)pbIcon, pvIcon, dwSizeInBytes);
#pragma pack(pop)
// save byte array pbIcon of size dwSizeInBytes
free(pbIcon);Nuclear launch detected
Thanks for the post. Does this work if the ICON is loaded in externally (e.g. from a .ico file) so is not a hardcoded resource in the application ? (I am using ExtractIcon() to create my icon from a file).
cheers, Neil
-
Thanks for the post. Does this work if the ICON is loaded in externally (e.g. from a .ico file) so is not a hardcoded resource in the application ? (I am using ExtractIcon() to create my icon from a file).
cheers, Neil
Probably yes, but I am not sure. Try using LoadImage instead of LoadIcon and pass the filename instead of MAKEINTRESOURCE, and use the returned HANDLE as HRSRC, but, again I'm not sure. But why not opening the .ico file with CreatFile directly, and store the file bytes directly?
Nuclear launch detected
-
Probably yes, but I am not sure. Try using LoadImage instead of LoadIcon and pass the filename instead of MAKEINTRESOURCE, and use the returned HANDLE as HRSRC, but, again I'm not sure. But why not opening the .ico file with CreatFile directly, and store the file bytes directly?
Nuclear launch detected
Thanks again. Yes, I could use the .ico file as a "serialization". I can load an icon from a .ico file with ExtractIcon(), but there doesn't seem to be a reverse-function - to create the .ico file from an HICON.... do you know of a function to do this?
cheers, Neil
-
Thanks again. Yes, I could use the .ico file as a "serialization". I can load an icon from a .ico file with ExtractIcon(), but there doesn't seem to be a reverse-function - to create the .ico file from an HICON.... do you know of a function to do this?
cheers, Neil
Probably CreateIcon, CreateIconFromResource and CreateIconFromResourceEx will help you. Bytes will come yet again from LoadResource. Also, CreateIconIndirect + ICONINFO could help if you have a bitmap instead.
Nuclear launch detected