Solved

HICON icon handle - save icon to HDD

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now