Click here to Skip to main content
Licence 
First Posted 23 Apr 2002
Views 115,307
Bookmarked 39 times

CreateGrayscaleIcon

By | 20 May 2002 | Article
A Win32 function to create grayscale icons

Sample Image - CreateGrayscaleIcon.png

Abstract

CreateGrayscaleIcon is a Win32 function that creates a grayscale icon starting from a given icon.
The resulting icon will have the same size of the original one. This function is written using only Win32 APIs
to make possible to be used either in MFC and pure Win32 applications.

Function description

Just add in your project a function like the following:

// This function creates a grayscale icon starting from a given icon.
// The resulting icon will have the same size of the original one.
//
// Parameters:
//      [IN]    hIcon
//              Handle to the original icon.
//
// Return value:
//      If the function succeeds, the return value is the handle to 
//      the newly created grayscale icon.
//      If the function fails, the return value is NULL.
//
HICON CreateGrayscaleIcon(HICON hIcon)
{
    HICON       hGrayIcon = NULL;
    HDC         hMainDC = NULL, hMemDC1 = NULL, hMemDC2 = NULL;
    BITMAP      bmp;
    HBITMAP     hOldBmp1 = NULL, hOldBmp2 = NULL;
    ICONINFO    csII, csGrayII;
    BOOL        bRetValue = FALSE;

    bRetValue = ::GetIconInfo(hIcon, &csII);
    if (bRetValue == FALSE) return NULL;

    hMainDC = ::GetDC(NULL);
    hMemDC1 = ::CreateCompatibleDC(hMainDC);
    hMemDC2 = ::CreateCompatibleDC(hMainDC);
    if (hMainDC == NULL || hMemDC1 == NULL || hMemDC2 == NULL) return NULL;
  
    if (::GetObject(csII.hbmColor, sizeof(BITMAP), &bmp))
    {
        DWORD   dwWidth = csII.xHotspot*2;
        DWORD   dwHeight = csII.yHotspot*2;

        csGrayII.hbmColor = ::CreateBitmap(dwWidth, dwHeight, bmp.bmPlanes, 
                                           bmp.bmBitsPixel, NULL);
        if (csGrayII.hbmColor)
        {
            hOldBmp1 = (HBITMAP)::SelectObject(hMemDC1, csII.hbmColor);
            hOldBmp2 = (HBITMAP)::SelectObject(hMemDC2, csGrayII.hbmColor);

            //::BitBlt(hMemDC2, 0, 0, dwWidth, dwHeight, hMemDC1, 0, 0, 
            //         SRCCOPY);

            DWORD    dwLoopY = 0, dwLoopX = 0;
            COLORREF crPixel = 0;
            BYTE     byNewPixel = 0;

            for (dwLoopY = 0; dwLoopY < dwHeight; dwLoopY++)
            {
                for (dwLoopX = 0; dwLoopX < dwWidth; dwLoopX++)
                {
                    crPixel = ::GetPixel(hMemDC1, dwLoopX, dwLoopY);

                    byNewPixel = (BYTE)((GetRValue(crPixel) * 0.299) + 
                                        (GetGValue(crPixel) * 0.587) + 
                                        (GetBValue(crPixel) * 0.114));
                    if (crPixel) ::SetPixel(hMemDC2, dwLoopX, dwLoopY, 
                                            RGB(byNewPixel, byNewPixel, 
                                            byNewPixel));
                } // for
            } // for

            ::SelectObject(hMemDC1, hOldBmp1);
            ::SelectObject(hMemDC2, hOldBmp2);

            csGrayII.hbmMask = csII.hbmMask;

            csGrayII.fIcon = TRUE;
            hGrayIcon = ::CreateIconIndirect(&csGrayII);
        } // if

        ::DeleteObject(csGrayII.hbmColor);
        //::DeleteObject(csGrayII.hbmMask);
    } // if

    ::DeleteObject(csII.hbmColor);
    ::DeleteObject(csII.hbmMask);
    ::DeleteDC(hMemDC1);
    ::DeleteDC(hMemDC2);
    ::ReleaseDC(NULL, hMainDC);

    return hGrayIcon;
} // End of CreateGrayscaleIcon

History

  • 03/May/2002
    Removed dependancy from m_hWnd
    Removed 1 BitBlt operation
  • 16/April/2002
    First release

Disclaimer

THE SOFTWARE AND THE ACCOMPANYING FILES ARE DISTRIBUTED "AS IS" AND WITHOUT ANY WARRANTIES WHETHER EXPRESSED OR IMPLIED. NO RESPONSIBILITIES FOR POSSIBLE DAMAGES OR EVEN FUNCTIONALITY CAN BE TAKEN. THE USER MUST ASSUME THE ENTIRE RISK OF USING THIS SOFTWARE.

Terms of use

THIS SOFTWARE IS FREE FOR PERSONAL USE OR FREEWARE APPLICATIONS.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Davide Calabro

Web Developer

Italy Italy

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralSmall bugs PinmembertHeWiZaRdOfDoS12:45 31 Oct '06  
QuestionFail to use this function Pinmemberbinglan21:16 14 Aug '06  
Generalsmall question... PinmemberMax Santos6:54 7 Oct '04  
GeneralRe: small question... PinmemberDavide Calabro21:02 7 Oct '04  
GeneralBug PinmemberMichael Kaufmann2:57 21 Sep '04  
GeneralIy's just fit me. thanks a lot. Pinmember下眩月16:45 26 Sep '03  
GeneralGood work Pinmemberjawad ayub baig20:05 22 May '02  
GeneralVery Usefully Thanks PinmemberAnonymous17:31 27 Apr '02  
GeneralSemi Related Question PinmemberNorm Almond22:18 24 Apr '02  
GeneralRe: Semi Related Question PinmemberDavide Calabro3:49 25 Apr '02  
GeneralRe: Semi Related Question PinmemberEmmanuel Deloget21:41 29 Apr '02  
GeneralUseful. PinmemberShog99:41 24 Apr '02  
GeneralRe: Useful. PinmemberDavide Calabro10:36 24 Apr '02  
QuestionHey, why you focus only on m_hWnd ? PinmemberDavide Calabro3:58 24 Apr '02  
AnswerRe: Hey, why you focus only on m_hWnd ? PinmemberEd Gadziemski8:27 24 Apr '02  
Instead of:
 
  hMainDC = ::GetDC(m_hWnd);
  hMemDC1 = ::CreateCompatibleDC(hMainDC);
  hMemDC2 = ::CreateCompatibleDC(hMainDC);
  if (hMainDC == NULL || hMemDC1 == NULL || hMemDC2 == NULL) return NULL; 
 
you could do:
 
  hMainDC = ::GetDC(m_hWnd);
  if (hMainDC == NULL) return NULL;
  hMemDC1 = ::CreateCompatibleDC(hMainDC);
  hMemDC2 = ::CreateCompatibleDC(hMainDC);
  if (hMemDC1 == NULL || hMemDC2 == NULL) return NULL; 
 
Since your concern seems to be not failure to obtain hMainDC, it would be more efficient to bail out as soon as you know it failed instead of waiting until after you try to create the two compatible DCs using a null value.
 
Ed
GeneralRe: Hey, why you focus only on m_hWnd ? PinmemberDavide Calabro10:38 24 Apr '02  
AnswerRe: Hey, why you focus only on m_hWnd ? PinmemberEd Gadziemski8:33 24 Apr '02  
GeneralRe: Hey, why you focus only on m_hWnd ? PinmemberDavide Calabro10:38 24 Apr '02  
AnswerSorry Davide PinmemberJean-Michel LE FOL21:15 24 Apr '02  
GeneralRe: Sorry Davide PinmemberDavide Calabro3:46 25 Apr '02  
Questionand m_hWnd, is it pure Win32 Application ??? PinmemberJean-Michel LE FOL22:33 23 Apr '02  
AnswerRe: and m_hWnd, is it pure Win32 Application ??? PinmemberRobert W.0:45 24 Apr '02  
GeneralRe: and m_hWnd, is it pure Win32 Application ??? PinmemberRobert W.0:47 24 Apr '02  
GeneralRe: and m_hWnd, is it pure Win32 Application ??? PinmemberMaximilian Hänel1:11 24 Apr '02  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 21 May 2002
Article Copyright 2002 by Davide Calabro
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid