Domanda

GetDIBits() was not passing the correct BGR values to a COLORREF array:

#include <windows.h>
#include <iostream>
using namespace std;

int main() {int i; HBITMAP hBit; HDC bdc; BITMAPINFO bmpInfo; COLORREF pixel[100];


    hBit=(HBITMAP)LoadImage(NULL,(LPCTSTR)"F:\\bitmap.bmp",IMAGE_BITMAP,10,10,LR_LOADFROMFILE);
    bdc=CreateCompatibleDC(NULL);
    SelectObject(bdc,hBit);


    bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFO);
    bmpInfo.bmiHeader.biWidth=10;
    bmpInfo.bmiHeader.biHeight=-10;
    bmpInfo.bmiHeader.biPlanes=1;
    bmpInfo.bmiHeader.biBitCount=24;
    bmpInfo.bmiHeader.biCompression=BI_RGB;
    bmpInfo.bmiHeader.biSizeImage=0;


    GetDIBits(bdc,hBit,0,10,pixel,&bmpInfo,DIB_RGB_COLORS);


    for (i=0; i<100; i++) {
        cout<<GetBValue(pixel[i]);
        cout<<GetGValue(pixel[i]);
        cout<<GetRValue(pixel[i]);
        cout<<endl;
    }


    ReleaseDC(NULL,bdc);
    DeleteDC(bdc);
    DeleteObject(hBit);
    free(pixel);
    while (1) {}
}

bitmap.bmp is an entirely blue (RGB(0,0,255)) 10x10 24-bit bitmap file. The first few lines of the output look like:

0 0 255

255 0 0

0 255 0

0 0 255

And it's not only the order of the values that changes; some color values are 0 when they shouldn't be. The last few COLORREF values are RGB(0,0,0). What could be the problem with the code?

È stato utile?

Soluzione

It looks like you values are shifted, probably because you're missing a byte.

You should check that the BMP file is actually a 24bit RGB bitmap, and not something like 32bit RGBA.

Try putting a bit count of 32 instead of 24, there may be an unused byte in the your BMP pixels :

bmpInfo.bmiHeader.biBitCount = 32;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top