I am extracting an icon from .exe/.dll and want to save it in an .ico file. What is the best way to do this?
I have tried to use ::OleCreatePictureIndirect()
and then IPicture->SaveAsFile()
. It works but transparent parts of the icon are painted black (and obviously are not transparent any more :( ).
I tried manual parsing. It works OK but is cumbersome and I am afraid of complications with Vista icons/.icl files/etc.
Please, help. Thanks.
I had the same problem and therefore I wrote a function to save icons from an HICON handle to a ICO file. The
SaveIcon()
function shown below support colors depths of 4, 8, 24, and 32 bits. The PNG icon format is not supported.The function works by constructing the ICO file directly. Fortunately, this is not too difficult, because the ICO format is almost identical to the BMP file format; furthermore, the BMP file format is almost identical to the in-memory representation returned by
GetDIBits()
.Here is the
SaveIcon()
function, together with a small test function (_tmain
):You can save
HICON
s with theIPicture::SaveAsFile()
method. Here's a sample C++ program that uses it:This code handles transparency correctly.