CBitmap bm;
bm.LoadBitmap(IDB_BITMAP1);
我想使此bm显示为灰色。

解决方案 »

  1.   

    请教一下,BitBlt的最后一个参数应该写什么?
      

  2.   

    nuaawenlin(飘人):我是要变灰,不是拷贝
      

  3.   

    dwRop 
    [in] Specifies a raster-operation code. These codes define how the color data for the source rectangle is to be combined with the color data for the destination rectangle to achieve the final color. 
    The following list shows some common raster operation codes. Value Description 
    BLACKNESS Fills the destination rectangle using the color associated with index 0 in the physical palette. (This color is black for the default physical palette.) 
    CAPTUREBLT Windows 98/Me, Windows 2000/XP: Includes any windows that are layered on top of your window in the resulting image. By default, the image only contains your window. Note that this generally cannot be used for printing device contexts. 
    DSTINVERT Inverts the destination rectangle. 
    MERGECOPY Merges the colors of the source rectangle with the brush currently selected in hdcDest, by using the Boolean AND operator. 
    MERGEPAINT Merges the colors of the inverted source rectangle with the colors of the destination rectangle by using the Boolean OR operator. 
    NOMIRRORBITMAP Windows 98/Me, Windows 2000/XP: Prevents the bitmap from being mirrored. 
    NOTSRCCOPY Copies the inverted source rectangle to the destination. 
    NOTSRCERASE Combines the colors of the source and destination rectangles by using the Boolean OR operator and then inverts the resultant color. 
    PATCOPY Copies the brush currently selected in hdcDest, into the destination bitmap. 
    PATINVERT Combines the colors of the brush currently selected in hdcDest, with the colors of the destination rectangle by using the Boolean XOR operator. 
    PATPAINT Combines the colors of the brush currently selected in hdcDest, with the colors of the inverted source rectangle by using the Boolean OR operator. The result of this operation is combined with the colors of the destination rectangle by using the Boolean OR operator. 
    SRCAND Combines the colors of the source and destination rectangles by using the Boolean AND operator. 
    SRCCOPY Copies the source rectangle directly to the destination rectangle. 
    SRCERASE Combines the inverted colors of the destination rectangle with the colors of the source rectangle by using the Boolean AND operator. 
    SRCINVERT Combines the colors of the source and destination rectangles by using the Boolean XOR operator. 
    SRCPAINT Combines the colors of the source and destination rectangles by using the Boolean OR operator. 
    WHITENESS Fills the destination rectangle using the color associated with index 1 in the physical palette. (This color is white for the default physical palette.) 
      

  4.   

    void DitherBlt(CDC *pDC, int nXDest, int nYDest, int nWidth, int nHeight, CBitmap &bitmap, int nXSrc, int nYSrc)
    { CDC dcMem;
    dcMem.CreateCompatibleDC(NULL); CBitmap bmpMem;
    bmpMem.CreateCompatibleBitmap(&dcMem, nWidth, nHeight);
    CBitmap* pbmpMem = dcMem.SelectObject(&bmpMem); CDC dcBitmap;
    dcBitmap.CreateCompatibleDC(NULL); CBitmap * pbmpOld = dcBitmap.SelectObject(&bitmap);
    CBrush brushShadow;
    CBrush brushHilight; dcMem.PatBlt(0, 0, nWidth, nHeight, WHITENESS);
    dcBitmap.SetBkColor(GetSysColor(COLOR_BTNFACE));
    dcMem.BitBlt(0, 0, nWidth, nHeight, &dcBitmap, nXSrc, nYSrc, SRCCOPY);
    dcBitmap.SetBkColor(GetSysColor(COLOR_BTNHILIGHT));
    dcMem.BitBlt(0, 0, nWidth, nHeight, &dcBitmap, nXSrc, nYSrc, SRCPAINT);
    dcBitmap.FillSolidRect(0, 0, nWidth, nHeight, GetSysColor((IsNewShell()) ? COLOR_3DFACE : COLOR_MENU));
    dcBitmap.SetBkColor(RGB(0, 0, 0));
    dcBitmap.SetTextColor(RGB(255, 255, 255));
    brushHilight.CreateSolidBrush(GetSysColor(COLOR_BTNHILIGHT));
    brushShadow.CreateSolidBrush(GetSysColor(COLOR_BTNSHADOW));
    CBrush * pbrushOld = dcBitmap.SelectObject(&brushHilight);
    dcBitmap.BitBlt(0, 0, nWidth, nHeight, &dcMem, 0, 0, 0x00E20746L);
    pDC->BitBlt(nXDest+1,nYDest+1,nWidth, nHeight, &dcBitmap,0,0,SRCCOPY);
    dcBitmap.BitBlt(1, 1, nWidth, nHeight, &dcMem, 0, 0, 0x00E20746L);
    dcBitmap.SelectObject(&brushShadow);
    dcBitmap.BitBlt(0, 0, nWidth, nHeight, &dcMem, 0, 0, 0x00E20746L);
    pDC->BitBlt(nXDest, nYDest, nWidth, nHeight, &dcBitmap, 0, 0, SRCCOPY); dcMem.SelectObject(pbmpMem);
    dcBitmap.SelectObject(pbrushOld);
    dcBitmap.SelectObject(pbmpOld);
    dcMem.DeleteDC();
    dcBitmap.DeleteDC();
    bmpMem.DeleteObject();
    }
      

  5.   

    goodseener(西湖醋鱼) 你的代码我试过了,不行呀
      

  6.   

    看看下面的函数,记得不用了要销毁返回的句炳。HBITMAP GrayScale(CDC *pDC, HBITMAP hBitmap)
    {
        // get the bitmap's size and colour information
        BITMAP bm;
        if (NULL == hBitmap || ::GetObject(hBitmap, sizeof(BITMAP), &bm) == 0)
    return 0;    // create a DIBSection copy of the original bitmap
        HBITMAP hDib = (HBITMAP)::CopyImage(hBitmap, IMAGE_BITMAP, 0, 0, LR_COPYRETURNORG | LR_CREATEDIBSECTION);
        if (bm.bmBitsPixel < 16)
        {   // bitmap has a colour table, so we modify the colour table
            CDC memDC;
            memDC.CreateCompatibleDC(pDC);
            int SavedMemDC = memDC.SaveDC();
            memDC.SelectObject(hDib);
            int nColours = 1 << bm.bmBitsPixel;
            RGBQUAD pal[256];
            // Get the colour table
            ::GetDIBColorTable(memDC.m_hDC, 0, nColours, pal);
            // modify the colour table
            for (int x = 0; x < nColours; x++)
            {
                BYTE nGray = (BYTE)Gray(pal[x].rgbRed, pal[x].rgbGreen, pal[x].rgbBlue);
                pal[x].rgbRed = nGray;
                pal[x].rgbGreen = nGray;
                pal[x].rgbBlue = nGray;
            }
            // set the modified colour tab to the DIBSection bitmap
            ::SetDIBColorTable(memDC.m_hDC, 0, nColours, pal);
            memDC.RestoreDC(SavedMemDC);
            memDC.DeleteDC();
            return hDib;
        }    else
        {   // the bitmap does not have a colour table, so we modify the bitmap bits directly
            int Size = bm.bmHeight * bm.bmWidth;
            BITMAPINFO bmi;
            bmi.bmiHeader.biSize          = sizeof(BITMAPINFOHEADER);
            bmi.bmiHeader.biHeight        = bm.bmHeight;
            bmi.bmiHeader.biWidth         = bm.bmWidth;
            bmi.bmiHeader.biPlanes        = 1;
            bmi.bmiHeader.biBitCount      = bm.bmBitsPixel;
            bmi.bmiHeader.biCompression   = BI_RGB;
            bmi.bmiHeader.biSizeImage     = ((bm.bmWidth * bm.bmBitsPixel + 31) & (~31)) / 8 * bm.bmHeight;
            bmi.bmiHeader.biXPelsPerMeter = 0;
            bmi.bmiHeader.biYPelsPerMeter = 0;
            bmi.bmiHeader.biClrUsed       = 0;
            bmi.bmiHeader.biClrImportant  = 0;
            
            // Get the bitmaps data bits
            BYTE *pBits = new BYTE[bmi.bmiHeader.biSizeImage];
            VERIFY (::GetDIBits(pDC->m_hDC, hDib, 0, bm.bmHeight, pBits, &bmi, DIB_RGB_COLORS));        if (bm.bmBitsPixel == 32)
            {
                DWORD *dst=(DWORD *)pBits;
                while (Size--)
                {
    int nGray = Gray(GetBValue(*dst), GetGValue(*dst), GetRValue(*dst));
                    *dst = (DWORD)RGB(nGray, nGray, nGray);
                    dst++;
               }
            }
            else if (bm.bmBitsPixel == 24)
            {
                BYTE *dst=(BYTE*)pBits;
                for (int dh = 0; dh < bm.bmHeight; dh++)
                {
                    for (int dw = 0; dw < bm.bmWidth; dw++)
                    {
                        int nGray = Gray(dst[2], dst[1], dst[0]);
                        dst[0]=(BYTE)nGray;
                        dst[1]=(BYTE)nGray;
                        dst[2]=(BYTE)nGray;
                        dst += 3;
                    }
                    // each row is DWORD aligned, so when we reach the end of a row, we
                    // have to realign the pointer to point to the start of the next row
                    int pos = (int)dst - (int)pBits;
                    int rem = pos % 4;
                    if (rem)
                        dst += 4 - rem;
                }
            }
            else if (bm.bmBitsPixel == 16)
            {
                WORD *dst=(WORD*)pBits;
                while (Size--)
                {
                    BYTE b = (BYTE)((*dst)&(0x1F));
                    BYTE g = (BYTE)(((*dst)>>5)&(0x1F));
                    BYTE r = (BYTE)(((*dst)>>10)&(0x1F));
                    
                    int nGray = Gray(r, g, b);
                    *dst = ((WORD)(((BYTE)(nGray)|((WORD)((BYTE)(nGray))<<5))|(((DWORD)(BYTE)(nGray))<<10)));
                    
                    dst++;
                }
            }
            // set the modified bitmap data bits to the DIBSection
            ::SetDIBits(pDC->m_hDC, hDib, 0, bm.bmHeight, pBits, &bmi, DIB_RGB_COLORS);
            delete[] pBits;
            return hDib;
        }
    }