Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

HICON icon handle - save icon to HDD

Posted on 2007-03-18
3
Medium Priority
?
989 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1500 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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. …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

721 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