Creating .ico file from HICON
-
In a previous post I was attempting to "serialize" an HICON, i.e. save it as a blob in a file or database: the opposite operation to the ExtractIcon() operation. I have since learned how to do this (see routine below). BUT now my problem is, this routine loses color information. When the routine is run against a true-color icon, and the buffer is saved as a .ico file, and the .ico file is reloaded with ExtractIcon(), the resulting icon has reduced to 256 colors. I think this must be a limitation of OleCreatePictureIndirect(). How can I fix my routine? ------------------------------------------------ void SerializeIcon(const HICON icon, DWORD* size, BYTE** data) { LPPICTURE pPicture; PICTDESC rPD; rPD.cbSizeofstruct = sizeof(PICTDESC); rPD.picType = PICTYPE_ICON; rPD.icon.hicon = icon; IStream* pStream = NULL; HGLOBAL hMem = NULL; BYTE* pMem = NULL; long lActual; OleCreatePictureIndirect(&rPD, IID_IPicture, FALSE, (void**) &pPicture); CreateStreamOnHGlobal(0, TRUE, &pStream); pPicture->SaveAsFile(pStream, TRUE, &lActual); pPicture->Release(); GetHGlobalFromStream(pStream, &hMem); pMem = (BYTE*) GlobalLock(hMem); *size = GlobalSize(hMem); *data = (BYTE*) malloc(*size); CopyMemory(*data, pMem, *size); GlobalUnlock(hMem); GlobalFree(hMem); } ------------------------------------------------
cheers, Neil
-
In a previous post I was attempting to "serialize" an HICON, i.e. save it as a blob in a file or database: the opposite operation to the ExtractIcon() operation. I have since learned how to do this (see routine below). BUT now my problem is, this routine loses color information. When the routine is run against a true-color icon, and the buffer is saved as a .ico file, and the .ico file is reloaded with ExtractIcon(), the resulting icon has reduced to 256 colors. I think this must be a limitation of OleCreatePictureIndirect(). How can I fix my routine? ------------------------------------------------ void SerializeIcon(const HICON icon, DWORD* size, BYTE** data) { LPPICTURE pPicture; PICTDESC rPD; rPD.cbSizeofstruct = sizeof(PICTDESC); rPD.picType = PICTYPE_ICON; rPD.icon.hicon = icon; IStream* pStream = NULL; HGLOBAL hMem = NULL; BYTE* pMem = NULL; long lActual; OleCreatePictureIndirect(&rPD, IID_IPicture, FALSE, (void**) &pPicture); CreateStreamOnHGlobal(0, TRUE, &pStream); pPicture->SaveAsFile(pStream, TRUE, &lActual); pPicture->Release(); GetHGlobalFromStream(pStream, &hMem); pMem = (BYTE*) GlobalLock(hMem); *size = GlobalSize(hMem); *data = (BYTE*) malloc(*size); CopyMemory(*data, pMem, *size); GlobalUnlock(hMem); GlobalFree(hMem); } ------------------------------------------------
cheers, Neil
Did you find the answer to this question? I have the same problem.