Solved

HICON icon handle - save icon to HDD

Posted on 2007-03-18
3
964 Views
Last Modified: 2012-06-27
Hello,

I have a HICON icon handle, and I'd like to save it to HDD as an .ico file.
I haven't found any icon-saving function, nor HBITMAP-saving function.

I'm using Visual C++ 6.0 (SP6) MFC Dialog application.

Thanks.
0
Comment
Question by:JiriNovotny
  • 3
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 18743701
See the article at http://www.codeguru.com/cpp/g-m/bitmap/article.php/c1697/ ("Writing a bitmap to a BMP file"). the scoop is to

// WriteDIB            - Writes a DIB to file
// Returns            - TRUE on success
// szFile            - Name of file to write to
// hDIB                  - Handle of the DIB
BOOL WriteDIB( LPTSTR szFile, HANDLE hDIB)
{
      BITMAPFILEHEADER      hdr;
      LPBITMAPINFOHEADER      lpbi;

      if (!hDIB)
            return FALSE;

      CFile file;
      if( !file.Open( szFile, CFile::modeWrite|CFile::modeCreate) )
            return FALSE;

      lpbi = (LPBITMAPINFOHEADER)hDIB;

      int nColors = 1 << lpbi->biBitCount;

      // Fill in the fields of the file header
      hdr.bfType            = ((WORD) ('M' << 8) | 'B');      // is always "BM"
      hdr.bfSize            = GlobalSize (hDIB) + sizeof( hdr );
      hdr.bfReserved1       = 0;
      hdr.bfReserved2       = 0;
      hdr.bfOffBits            = (DWORD) (sizeof( hdr ) + lpbi->biSize +
                                    nColors * sizeof(RGBQUAD));

      // Write the file header
      file.Write( &hdr, sizeof(hdr) );

      // Write the DIB header and the bits
      file.Write( lpbi, GlobalSize(hDIB) );

      return TRUE;
}

Since icons and bitmaps use the same format, that works for icons too.
0
 
LVL 86

Expert Comment

by:jkr
ID: 18743719
BTW, see also http://msdn2.microsoft.com/en-us/library/ms997538.aspx ("Icons in Win32"). The sample code attached to the article (http://download.microsoft.com/download/win95/utility/1.0/w9xxp/en-us/4493.exe) should prove quite useful.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 18743758
Just found that one: http://www.codeproject.com/win32/IconExtraction.asp ("Icon extraction made easy") which also could be interesting for you.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question