0

I'm trying to load and draw an icon from an .ICO file that contains multiple icons. I can't figure out how to load a specific icon. I'm using LoadImage as in the following code

    {
      char *iconfile="myicon.ico";
      int w=72,h=72;
      hIcon = LoadImage( hMainInst, iconfile, IMAGE_ICON, w, h, LR_LOADFROMFILE );  
    }

This works Ok but the only way I can see to select a specific icon is by size, e.g., 72x72 in my example. It seems to load the first 72x72 icon in the file. The problem is that there are multiple 72x72 icons in the file.

How do I load an icon other than the first?

DontPanic
  • 2,164
  • 5
  • 29
  • 56
  • Why would you want to do this? Usually you have multiple icons of the same size because they have different colour depths. But surely you want the system to choose the appropriate one for your screen. Or do you have an ico file with different logical icons? – David Heffernan Jan 18 '21 at 18:05
  • David: I'm writing a utility app to list/extract all icons in a file, for development purposes. For use in some app, I agree that letting the system choose is the way to go. – DontPanic Jan 18 '21 at 18:21
  • In that case you should parse the ICO file. This isn't too hard. There is documentation and lots of code samples kicking around the web. – David Heffernan Jan 18 '21 at 18:25
  • Documentation on ICON resources is pretty scarce, to the point where outside Raymond Chen's blog there is no information at all concerning PNG icon files. To my knowledge anyway. – IInspectable Jan 18 '21 at 20:59
  • Can [ExtractIconEx](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-extracticonexa) work for you? – Zeus Jan 19 '21 at 06:36
  • David: Yes, I wrote an app to parse an icon file to get the directory header and all entries, but (I don't think) that gets me icon *handles* that I can use to draw icons with DrawIcon(). I suppose I could read the bitmapinfo and pixels and draw from those but that seems too painful. – DontPanic Jan 19 '21 at 20:52
  • Zhu: I could not get ExtractIconEx to work for me. Maybe a dumb error but I only get ONE icon handle despite specifying nIcons=10 for example. And I know there are LOTS of icons in my test exe file. – DontPanic Jan 19 '21 at 20:56
  • It is not sure what your file is, but you can refer to this [thread](https://stackoverflow.com/questions/23263599/how-to-extract-128x128-icon-bitmap-data-from-exe-in-python) to try to parse the ico file. – Zeus Jan 20 '21 at 02:48
  • "I suppose I could read the bitmapinfo and pixels and draw from those but that seems too painful." I don't see that you gave an alternative. – David Heffernan Jan 20 '21 at 03:43

0 Answers0