[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3066
  • Last Modified:

Save hicon to a .ico file

Q: How to save an HICON to a .ico file

VC6, Needs to work on Win 95 and up. Can be MFC or Win API.

Ideally work for whatever the hicon contains, but failing that a version which works with 32X32 16 color icons would do.



BOOL MySaveIcon( hIcon, LPCSTR lpszFile )
{
// <-------what goes here
}


int main( int argc, char * argv[] )
{
HICON hIcon ;
// etc. etc. lots of stuff setting up hIcon

MySaveIcon( hIcon "c:\\example.ico" ) ;
}
0
Answers2000
Asked:
Answers2000
  • 3
  • 3
  • 2
  • +2
1 Solution
 
AxterCommented:
HANDLE hIcon; //handle to Icon
int nHandleSize = ::GlobalSize(hIcon);
LPSTR lpIcon = ::GlobalLock(hIcon);

//simply write nHandleSize bytes of the data in lpIcon to
// the file on the disk. Don't forget to do

::GlobalUnlock(hIcon);

0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jkrCommented:
Hum, that's actually pretty much what Axter described...
0
 
AxterCommented:
jkr,
It should be.
I copied it off one of your previous answers. :-) :-O
0
 
Answers2000Author Commented:
Michael, nope, it needs to be ico format

Axter/JKR - if this work'd it be great - funny that I get 0 back for nHandleSize (in nAxter's ver), I'll go back and check the rest of the stuff to figure out if it's me or this method simply doesn't work in win32...  btw I'm testing on Win98 at the moment, but it needs to work on everything from 95 and NT4 upwards.

In case you're wondering, I'm adding a mini-icon editor to my prog (MFC) as a "hobby" (i.e. not much time to work on this) in between doing the real work on it. The darn thing only needs to work on 32X32 16 color icons, and this one last thing to solve.

0
 
jkrCommented:
>>It should be.
>>I copied it off one of your previous answers. :-) :-O

It'd better be one graded "A" :o)
0
 
AxterCommented:
Answers2000,
Can you post the code you have in the relevant section?
0
 
Answers2000Author Commented:
Sorry for delay

I'm sure your answer is right.

The test program I have uses all this library of code (not by me!) to make the icon, and this is when I couldn't make it save.   The bug seems to be in the library, which I don't have time to debug - so I've shelved this hobby project for the time being.
0
 
eliakarisCommented:
I am trying to use this code in c# using .NET interop and this does not seem to be working.

I am using the Handle property of the icon.
All I receive from the GlobalSize call is zero...

here are my P/Invokes...
[DllImport("kernel32.dll")]
static extern bool GlobalUnlock(IntPtr hMem);

[DllImport("kernel32.dll")]
static extern uint GlobalSize(IntPtr hMem);

[DllImport("kernel32.dll")]
static extern IntPtr GlobalLock(IntPtr hMem);

Doesnt work with LocalLock either.

Elia
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now