Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm writing a program which need to load icon resource from memory, there seems no direct windows API can do this work. the icon file was transmit by network, my program receives the data of the file, and store this data in my program buffer, I want to use this data to set the icon of a window. the code structure just like this:
C++
char* buffer;
recvicon_data_by_network(&buffer,...);
HICON icon=(HICON)how_to_load_icondata_from_memory(buffer,...);
SendMessage(my_hwnd,WM_SETICON,(WPARAM)ICON_SMALL,(LPARAM)icon);

As referred in MSDN, there is a structure ICONIFO
C++
typedef struct _ICONINFO {
  BOOL    fIcon;
  DWORD   xHotspot;
  DWORD   yHotspot;
  HBITMAP hbmMask;
  HBITMAP hbmColor;
} ICONINFO;

And the structure of .ICO file , it's a little complicate, there seems have a lot about compression alg. I do not have the skills to deal with it yet.

What I have tried:

There are also some articles about convert .ico format file data to ICONINFO structure, I have tried, not work, maybe not the correct way.
C++
//size:16×16 icon
int cx = 16, cy = 16;
int offset=LookupIconIdFromDirectoryEx((PBYTE)&buffer[0], TRUE, cx, cy, LR_DEFAULTCOLOR);
unsigned short i=1, count = *((unsigned short*)&buffer[4]);
while (i < count) {
	memcpy(((PBYTE)&buffer[6]) + (i * 0xe),((PBYTE)&buffer[6]) + (i * 0x10), 0xe);
}

HICON ico_fav = CreateIconFromResourceEx((PBYTE)&buffer[offset], 0, TRUE, 0x00030000,cx,cy, LR_DEFAULTCOLOR);
I do not want to save the icon data in a local file, and my program structure does not support the way of gdiplus::Image::FromStream(...). I need the HICON handle and I have the memory of icon's data.
Posted
Updated 14-Mar-23 7:17am
Comments
Shao Voon Wong 14-Mar-23 21:07pm    
One workaround is to change your image format from ico to png, then you can use gdiplus::Image::FromStream(...).
Yount_0701 14-Mar-23 21:33pm    
Thank you.It's much simple if I use Gdiplus libs as you said.
1.Creating a IStream interface which consume the buffer which I recveived;
2.Using Gdiplus::Bitmap::FromStream(...);
3.Using Gdiplus::Bitmap::GetHICON(&my_icon);
perhaps this is the normal procedure of doing this work. Like I said, my program structure is not allowed to import Gdiplus libs for some reasons.

I am afraid it won't be an easy task. You might have a look at this article: Icons | Microsoft Learn[^].
 
Share this answer
 
I realize that you wrote you don't want to save the data to a file but I think that would be the easiest way to do it.

Maybe looking at how an image handling library does it will help you. Here's one that I use all the time and it handles icon files : CxImage[^]
 
Share this answer
 
Comments
Yount_0701 14-Mar-23 22:16pm    
Thank you. Your lib do a lot of work. That's great , which make me realize it's a huge work to have I want to be done. I have to considered if it's worth to workaround.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900