Solved

HICON icon handle - save icon to HDD

Posted on 2007-03-18
3
958 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

810 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