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. How to serialize an ICON

How to serialize an ICON

Scheduled Pinned Locked Moved C / C++ / MFC
databasejsontutorialquestion
8 Posts 3 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

    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

    W C 2 Replies Last reply
    0
    • N neilsolent

      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

      W Offline
      W Offline
      Waldermort
      wrote on last edited by
      #2

      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.

      N 1 Reply Last reply
      0
      • N neilsolent

        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

        C Offline
        C Offline
        Cristian Amarie
        wrote on last edited by
        #3

        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

        N 1 Reply Last reply
        0
        • W Waldermort

          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.

          N Offline
          N Offline
          neilsolent
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • C Cristian Amarie

            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

            N Offline
            N Offline
            neilsolent
            wrote on last edited by
            #5

            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

            C 1 Reply Last reply
            0
            • N neilsolent

              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

              C Offline
              C Offline
              Cristian Amarie
              wrote on last edited by
              #6

              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

              N 1 Reply Last reply
              0
              • C Cristian Amarie

                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

                N Offline
                N Offline
                neilsolent
                wrote on last edited by
                #7

                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

                C 1 Reply Last reply
                0
                • N neilsolent

                  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

                  C Offline
                  C Offline
                  Cristian Amarie
                  wrote on last edited by
                  #8

                  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

                  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