WM图片变灰处理

因为在Windows mobile 上没有

 GetBitmapBits和SetBitmapBits

所以只能用CreateDIBSection函数。

 

 

查看了些资料,入下:

 

///////////////////////////////////////////////////////////////////////////////////////////

wm

转载自:http://www.qqgb.com/Program/VC/VCJM/Program_137059.html

// 核心函数,将屏幕变暗

HBITMAP CMyFade::FadeBitmap(HBITMAP hBmp, double dfTrans)

{

HBITMAP hRetBmp = NULL;

if (hBmp)

{

HDC hBufferDC = CreateCompatibleDC(NULL);

HGDIOBJ hPrevBufObject = SelectObject(hBufferDC, hBmp);



HDC hDirectDC = CreateCompatibleDC(NULL); // DC for working

if (hDirectDC)

{

BITMAP bm;

GetObject(hBmp, sizeof(bm), &bm);

BITMAPINFO bmInfo;

ZeroMemory(&bmInfo,sizeof(bmInfo));

bmInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);

bmInfo.bmiHeader.biWidth = bm.bmWidth;

bmInfo.bmiHeader.biHeight = bm.bmHeight;

bmInfo.bmiHeader.biPlanes =  1;

bmInfo.bmiHeader.biBitCount = 32;

UINT* ptPixels;

HBITMAP hDirectBitmap = CreateDIBSection(hDirectDC,

(BITMAPINFO*)&bmInfo,

DIB_RGB_COLORS,(void**)&ptPixels, NULL, 0);

if (hDirectBitmap)

{

// 将hDirectBitmap放入hDirectDC中处理

HGDIOBJ hPrevBufDirObject = SelectObject(hDirectDC, hDirectBitmap);

// 当前将原hBmp即屏幕的所有像素写入到hDirectDC

// 即需要对像素灰度处理的DC中

BitBlt(hDirectDC,0,0,bm.bmWidth,bm.bmHeight,hBufferDC,0,0,SRCCOPY);



int iAlpha = (int)(255.0 * dfTrans / 100.0);



int nSize = bm.bmWidth * bm.bmHeight;

for (int i=0; i<nSize; i++)

{

// 0.212671 * R + 0.715160 * G + 0.072169 * B

int iSrcR = (ptPixels[i]) & 0x00ff0000 >> 16;

int iSrcG = ptPixels[i] & 0x0000ff00 >> 8;

int iSrcB = ptPixels[i] & 0x000000ff;

int iGrey = (iSrcR * 54 + iSrcG * 182 + iSrcB * 19) >> 8;



COLORREF Col =iGrey ; //RGB(iGrey, iGrey, iGrey) ;

ptPixels[i] = RGB(

(GetBValue( Col ) * iAlpha + iSrcB * (255 - iAlpha)) >> 8,

(GetGValue( Col ) * iAlpha + iSrcG * (255 - iAlpha)) >> 8,

(GetRValue( Col ) * iAlpha + iSrcR * (255 - iAlpha)) >> 8 );

}

SelectObject(hDirectDC,hPrevBufDirObject);

hRetBmp = hDirectBitmap;

}

DeleteDC(hDirectDC);

}

SelectObject(hBufferDC, hPrevBufObject);

DeleteDC(hBufferDC);

}

return hRetBmp;

}

 

/////////////////////////////////////////////////////////////////

vc

BITMAP bm;

        GetObject(bitmap1, sizeof(BITMAP), &bm);

        BYTE *pData = new BYTE[bm.bmWidthBytes * bm.bmHeight];

        GetBitmapBits(bitmap1,  bm.bmWidthBytes * bm.bmHeight, pData);

        int bits = bm.bmWidthBytes / bm.bmWidth;
        

        for (int i = m_Shape.lMinY; i < m_Shape.lMaxY; i++)
        {
            
            for (int k = m_Shape.lMinX * bits; k < m_Shape.lMaxX * bits; k += bits)
            {
                BYTE Gray = pData[i * bm.bmWidthBytes + k] * 0.3 + pData[i * bm.bmWidthBytes + k + 1] * 0.59 + pData[i * bm.bmWidthBytes + k + 2] * 0.11;

                

                if (Gray + nGrade > 255)
                {
                    Gray = 255;
                }
                else
                {
                    if (Gray + nGrade < 0)
                    {
                        Gray = 0;
                    }
                    else
                    {
                        Gray += nGrade;
                    }
                }
                

                pData[i * bm.bmWidthBytes + k]  = Gray;
                pData[i * bm.bmWidthBytes + k + 1]  = Gray;
                pData[i * bm.bmWidthBytes + k + 2]  = Gray;
            }
        }

        SetBitmapBits(bitmap1,  bm.bmWidthBytes * bm.bmHeight, pData);<

你可能感兴趣的:(windows,null,mobile,byte,colors)