WINCE에서 HBITMAP에서 비트맵 RGB 색상 데이터 가져오기

4478 단어 데이터RGBHBITMAP
         ,        ,        ,        :
class CBitmapBits
{
public:
    CBitmapBits()
        : m_pBitsBuf(NULL)
        , m_dwWidth(0)
        , m_dwHeight(0)
    { 

    }

    ~CBitmapBits()
    {
       Destroy();
    }

    //       
    void Destroy()
    {
        if (m_pBitsBuf != NULL)
        {
            delete[] m_pBitsBuf;
            m_pBitsBuf = NULL;
        }
        m_dwWidth = 0;
        m_dwHeight = 0;
    }

public:
    BYTE *m_pBitsBuf;    //   RGB  ,      , 4    ,    :  *  *3
    DWORD m_dwWidth;     //     
    DWORD m_dwHeight;    //     
};

//  HBITMAP   DC   
BOOL
DrawHBitmapToDC(
    IN  HBITMAP hBitmap,               //     
    IN  HDC hDC                        //    DC
    )
{
    //      
    if (hBitmap==NULL || hDC==NULL)
    {
        return FALSE;
    }

    //         ,     
    BITMAP bmpObj = {0};
    if (   ::GetObject(hBitmap, sizeof(bmpObj), &bmpObj) == 0
        || bmpObj.bmWidth <= 0
        || bmpObj.bmHeight <= 0
        )
    {
        return FALSE;
    }

    //     DC
    HDC hMemDC = ::CreateCompatibleDC(hDC);			
    if (hMemDC == NULL)
    {
        return FALSE;
    }

    //  HBITMAP    DC
    HBITMAP hOldBitmap = (HBITMAP)::SelectObject(hMemDC, hBitmap);

    //    DC       DC 
    ::BitBlt(hDC, 0, 0, bmpObj.bmWidth, bmpObj.bmHeight, hMemDC, 0, 0, SRCCOPY);

    //     DC
    ::SelectObject(hMemDC, hOldBitmap);
    ::DeleteDC(hMemDC);           

    //     
    return TRUE;
}

//  HBITMAP      
BOOL 
GetHBitmapBits(
    IN  HBITMAP hBitmap,               //     
    OUT CBitmapBits &bitmapBits        //      
    )
{
    //        
    bitmapBits.Destroy();

    //      
    if (hBitmap == NULL)
    {
        return FALSE;
    }

    //         ,     
    BITMAP bmpObj = {0};
    if (   ::GetObject(hBitmap, sizeof(bmpObj), &bmpObj) == 0
        || bmpObj.bmWidth <= 0
        || bmpObj.bmHeight <= 0
        )
    {
        return FALSE;
    }

    //   DIB  DC
    HDC hDIBDC = ::CreateCompatibleDC(NULL);  
    if (hDIBDC == NULL)
    {
        return FALSE;
    }

    //   DIB    
    BITMAPINFO hdr;   
    ZeroMemory(&hdr , sizeof(BITMAPINFO));  
    hdr.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);  
    hdr.bmiHeader.biWidth = bmpObj.bmWidth;  
    hdr.bmiHeader.biHeight = -bmpObj.bmHeight;  //                   
    hdr.bmiHeader.biPlanes = 1;  
    hdr.bmiHeader.biBitCount = 32;  
    BYTE * pbtPixels = NULL;   
    HBITMAP hDIBitmap = ::CreateDIBSection(hDIBDC, (BITMAPINFO *)&hdr, DIB_RGB_COLORS, (void **)&pbtPixels, NULL, 0);  
    if (hDIBitmap == NULL)
    {
        ::DeleteDC(hDIBDC);
        return FALSE;
    }

    //  DIB    DIB  DC
    HBITMAP hOldDIBBmp = (HBITMAP)::SelectObject(hDIBDC, hDIBitmap);  

    //  HBITMAP   DIB  DC 
    if (DrawHBitmapToDC(hBitmap, hDIBDC) == FALSE)
    {
        ::SelectObject(hDIBDC, hOldDIBBmp);
        ::DeleteDC(hDIBDC);
        ::DeleteObject(hDIBitmap);
        return FALSE;
    }

    //  pbtPixels          bitmapBits 
    //         
    bitmapBits.m_pBitsBuf = new BYTE[bmpObj.bmWidth*bmpObj.bmHeight*3];
    if (bitmapBits.m_pBitsBuf == NULL)
    {
        ::SelectObject(hDIBDC, hOldDIBBmp);
        ::DeleteDC(hDIBDC);
        ::DeleteObject(hDIBitmap);
        return FALSE;
    }

    //         
    bitmapBits.m_dwWidth = bmpObj.bmWidth;
    bitmapBits.m_dwHeight = bmpObj.bmHeight;

    //          RGB  
    long lSrcRowStartPos = 0;
    long lSrcPos = 0;
    long lDstRowStartPos = 0;
    long lDstPos = 0;
    for (long lRowIndex=0; lRowIndex<bmpObj.bmHeight; lRowIndex++)
    {
        //       
        lSrcRowStartPos = lRowIndex * bmpObj.bmWidth * 4;
        lDstRowStartPos = lRowIndex * bmpObj.bmWidth * 3;

        //      
        for (long lColumnIndex=0; lColumnIndex<bmpObj.bmWidth; lColumnIndex++)
        {
            //             ,        
            lSrcPos = lSrcRowStartPos + lColumnIndex*4;
            lDstPos = lDstRowStartPos + lColumnIndex*3;
            
            //      BGRA 4    ,     RGB  
            bitmapBits.m_pBitsBuf[lDstPos] = pbtPixels[lSrcPos+2];
            bitmapBits.m_pBitsBuf[lDstPos+1] = pbtPixels[lSrcPos+1];
            bitmapBits.m_pBitsBuf[lDstPos+2] = pbtPixels[lSrcPos];
        }
    }

    //   DIB  DC   
    ::SelectObject(hDIBDC, hOldDIBBmp);
    ::DeleteDC(hDIBDC);
    ::DeleteObject(hDIBitmap);

    //       
    return TRUE;
}

좋은 웹페이지 즐겨찾기