用GetBitmapBits保存图片信息到数组中,每个点对应一个int,其中的r、g、b是怎么分配的?我使用时怎么总是颜色有偏差,好像遗漏了什么信息看看是哪里不对啊 GetBitmapBits(hbit,sizeof(int)*width*height,bmpbits);
for (i=0;i<width*height;i++) 
{
j=*(bmpbits+i);
r = j & 255;
g = (j / 256) & 255;
b = (j / 65536) & 255;
//a = (j / 256/256) & 255;
r=255-r;
g=255-g;
b=255-b;
*(bmpbits+i)=RGB(r,g,b);
}

解决方案 »

  1.   

    MSDN中有提示的啊。
    颜色有几位,是在bitmapinfoheader中看的吧。GetBitmapBits
    The GetBitmapBits function copies the bitmap bits of a specified bitmap into a buffer. Note  This function is provided only for compatibility with 16-bit versions of Windows. Win32-based applications should use the GetDIBits function. LONG GetBitmapBits(
      HBITMAP hbmp,      // handle to bitmap
      LONG cbBuffer,     // number of bytes to copy
      LPVOID lpvBits     // buffer to receive bits
    );
    Parameters
    hbmp 
    [in] Handle to the bitmap of interest. 
    cbBuffer 
    [in] Specifies the number of bytes to copy from the bitmap into the buffer. 
    lpvBits 
    [out] Pointer to a buffer to receive the bitmap bits. The bits are stored as an array of byte values. 
    Return Values
    If the function succeeds, the return value is the number of bytes copied to the buffer.If the function fails, the return value is zero. Windows NT/ 2000: To get extended error information, call GetLastError. Requirements 
      Windows NT/2000: Requires Windows NT 3.1 or later.
      Windows 95/98: Requires Windows 95 or later.
      Header: Declared in Wingdi.h; include Windows.h.
      Library: Use Gdi32.lib.
      

  2.   

    GetBitmap(BITMAP* pBitMap)
    typedef struct tagBITMAP {  /* bm */
        int     bmType;
        int     bmWidth;
        int     bmHeight;
        int     bmWidthBytes;
        BYTE    bmPlanes;
        BYTE    bmBitsPixel;
        LPVOID  bmBits;
    } BITMAP;
      

  3.   

    如果你的图像是灰度图像,并且使用的ddb(设备无关位图,直接用系统调色板显示)读取方法的话,你的sizeof(int)应该定义为sizeof(BYTE)比较合适,因为256色每个想素点十一个字节。
    如果你用的是DIB读取得话,那么你应该创建了逻辑调色板吧?这样的话,灰读图像R=G=B,彩色则是不等。你最好定义一个nBytesWidth,(宽度上的字节数)nBitsPiexl(每个想素点的位数),建议你看看微软madn上面的diblook吧。讲的比较仔细。
    判断颜色的可以用一个messagebox讲这个数值显示出来,我记得有个函数可以得到位数的,我用过的,在madn上面查一下吧
      

  4.   

    16位的565是不是对应rgb那,那g的6位是怎么分配的,都是g?还是低5位是g,多余的一位又是什么呢
      

  5.   

    BYTE *lpBits;
    lpBits = (BYTE*)HeapAlloc(GetProcessHeap(), 0, 80*120*nChannels);
    GetBitmapBits(hBmp, 80*120*nChannels, lpBits); 
    IplImage* img = cvCreateImage(cvSize(80, 120), 8, nChannels);
    memset(img->imageData, 0, 80*120*nChannels);
    memcpy(img->imageData, (char*)lpBits, 80*120*nChannels);