Convert 32×32 icon to 16×16

CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Hi, finally I had the occasion to contribute in CodeGuru site. Well, this is
my humble solution to convert an icon from 32×32 format to 16×16. The procedure
is very simple, I get the two bitmaps of the icon (the image bitmap and the mask
bitmap) and I resize separately each bitmap from 32×32 to 16×16, with these two
new bitmaps obtained from the conversion is possible to make a new 16×16 icon
generated by the mother 32×32. Theorically the procedure is correct and it works well
enough; however, I noticed that the conversion made by Windows obtains the best
result compared with this, if anyone have an idea about to improve this
algorithm any suggestion will be welcomed.


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;
}

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read