Convert 32x32 icon to 16x16 | CodeGuru

Convert 32×32 icon to 16×16

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 […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 17, 2002
1 minute read
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;
}
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.