Convert 32x32 icon to 16x16

Hi, finally I had the occasion to contribute in CodeGuru site. Well, this is my humble solution to convert an icon from 32x32 format to 16x16. 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 32x32 to 16x16, with these two new bitmaps obtained from the conversion is possible to make a new 16x16 icon generated by the mother 32x32. 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;
}


Comments

  • Better results....

    Posted by Legacy on 07/04/2003 12:00am

    Originally posted by: Oskar Wieland

    You just have to call SetStretchBltMode( hMemDC2, HALFTONE ) before doing the StretchBlt.

    All the best
    Oskar

    Reply
  • Good! but...

    Posted by Legacy on 02/07/2003 12:00am

    Originally 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?

    Reply
  • Excellent ! ... How do you save both the 32x32 and the 16x16 into the ICO file

    Posted by Legacy on 01/24/2003 12:00am

    Originally 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.

    • sdf

      Posted by eran2 on 06/17/2009 02:15am

      sdf

      Reply
    Reply
  • How about CopyImage(...)?

    Posted by Legacy on 01/23/2002 12:00am

    Originally 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:00am

    Originally posted by: Diarrhio

    This helped a lot. Have been trying to figure this one out forever!

    D

    Reply
  • Better Looking Icons

    Posted by Legacy on 01/17/2002 12:00am

    Originally 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
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: March 19, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT The 2015 Enterprise Mobile Application Survey asked 250 mobility professionals what their biggest mobile challenges are, how many employees they are equipping with mobile apps, and their methods for driving value with mobility. Join Dan Woods, Editor and CTO of CITO Research, and Alan Murray, SVP of Products at Apperian, as they break down the results of this survey and discuss how enterprises are using mobile application management and private …

  • Companies undertaking an IT project need to find the right balance between cost and functionality. It's important to start by determining whether to build a solution from scratch, buy an out-of-the-box solution, or a combination of both. In reality, most projects will require some system tailoring to meet business requirements. Decision-makers must understand how much software development is enough and craft a detailed implementation plan to ensure the project's success. This white paper examines the different …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date