Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Creating .ico file from HICON

Creating .ico file from HICON

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiondatabasetutorialannouncement
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    neilsolent
    wrote on last edited by
    #1

    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

    T 1 Reply Last reply
    0
    • N neilsolent

      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

      T Offline
      T Offline
      tudorbalan
      wrote on last edited by
      #2

      Did you find the answer to this question? I have the same problem.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups