Convert 32x32 icon to 16x16
Posted
by Fabio Falsini
on January 17th, 2002
HICON Convert32x32IconTo16x16(HICON h32x32Icon)
{
HDC hMainDC, hMemDC1, hMemDC2;
HICON h16x16Icon;
BITMAP bmp;
HBITMAP hOldBmp1, hOldBmp2;
ICONINFO IconInfo32x32, IconInfo16x16;
GetIconInfo(h32x32Icon, &IconInfo32x32);
hMainDC = ::GetDC(m_hWnd);
hMemDC1 = CreateCompatibleDC(hMainDC);
hMemDC2 = CreateCompatibleDC(hMainDC);
GetObject(IconInfo32x32.hbmColor, sizeof(BITMAP), &bmp);
IconInfo16x16.hbmColor = CreateBitmap( 16, 16,
bmp.bmPlanes,
bmp.bmBitsPixel,
NULL);
hOldBmp1 = (HBITMAP) SelectObject( hMemDC1,
IconInfo32x32.hbmColor);
hOldBmp2 = (HBITMAP) SelectObject( hMemDC2,
IconInfo16x16.hbmColor);
StretchBlt(hMemDC2,
0, 0,
16, 16,
hMemDC1,
0, 0,
32, 32,
SRCCOPY
);
GetObject(IconInfo32x32.hbmMask, sizeof(BITMAP), &bmp);
IconInfo16x16.hbmMask = CreateBitmap( 16, 16,
bmp.bmPlanes,
bmp.bmBitsPixel,
NULL);
SelectObject(hMemDC1, IconInfo32x32.hbmMask);
SelectObject(hMemDC2, IconInfo16x16.hbmMask);
StretchBlt(hMemDC2,
0, 0,
16, 16,
hMemDC1,
0, 0,
32, 32,
SRCCOPY
);
SelectObject(hMemDC1, hOldBmp1);
SelectObject(hMemDC2, hOldBmp2);
IconInfo16x16.fIcon = TRUE;
h16x16Icon = CreateIconIndirect(&IconInfo16x16);
DeleteObject(IconInfo32x32.hbmColor);
DeleteObject(IconInfo16x16.hbmColor);
DeleteObject(IconInfo32x32.hbmMask);
DeleteObject(IconInfo16x16.hbmMask);
DeleteDC(hMemDC1);
DeleteDC(hMemDC2);
::ReleaseDC(m_hWnd, hMainDC);
return h16x16Icon;
}

Comments
Better results....
Posted by Legacy on 07/04/2003 12:00amOriginally posted by: Oskar Wieland
You just have to call SetStretchBltMode( hMemDC2, HALFTONE ) before doing the StretchBlt.
All the best
ReplyOskar
Good! but...
Posted by Legacy on 02/07/2003 12:00amOriginally posted by: Zhang
I have a problem.I want to generate a HICON from 2 HICONs. Can you tell me how can I get it?
ReplyExcellent ! ... How do you save both the 32x32 and the 16x16 into the ICO file
Posted by Legacy on 01/24/2003 12:00amOriginally posted by: Aharon Tam
Very helpfull article.
I intend to use to convert a 32 x 32 Icon Large Icon
to a 16 x 16 Small Icon.
However I wonder how save both icons into the same
*.ico file under res directory.
-
Replysdf
Posted by eran2 on 06/17/2009 02:15amsdf
ReplyHow about CopyImage(...)?
Posted by Legacy on 01/23/2002 12:00amOriginally posted by: Robert Moerland
I use CopyImage from the Win32 API to let Windows do the 32-to-16 scaling. It would be something like:
HICON 16x16Icon = (HICON) CopyImage(h32x32Icon, IMAGE_ICON, 16, 16, LR_COPYFROMRESOURCE);
The LR_COPYFROMRESOURCE flag makes sure that if there's a 16x16 icon, it is used instead of scaling a 32x32 icon.
Robert Moerland
Reply
Thank you
Posted by Legacy on 01/18/2002 12:00amOriginally posted by: Diarrhio
This helped a lot. Have been trying to figure this one out forever!
D
ReplyBetter Looking Icons
Posted by Legacy on 01/17/2002 12:00amOriginally posted by: Chris Richardson
Hi,
You had a question on how to possibly get better looking small icons. Under Windows NT and Windows 2000, you can set the stretching mode of the device context using the function call SetStretchBltMode. If you set the stretch mode to HALFTONE, you will probably get better small icons. You would probably want to add code to test for the version of the OS using GetVersionEx before attempting to set the mode though. Also, the MSDN says the HALFTONE mode is slower. In my own practice, I've noticed that the HALFTONE mode is about 3 times slower, however, with such small images, this difference is negligible.
Nice work,
Chris Richardson
Reply