GetDIBits()没有将正确的BGR值传递给COLORREF数组:
#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是完全蓝色(RGB(0,0,255)) 10x10 24位位图文件。输出的前几行如下所示:
0 0 255
255 0 0
0 255 %0
0 0 255
改变的不仅仅是值的顺序;有些颜色值不应该是0的。最后几个COLORREF值是RGB(0,0,0)。代码会有什么问题呢?
发布于 2013-05-17 16:25:43
看起来你的值被移位了,可能是因为你少了一个字节。
您应该检查BMP文件是否实际上是24bit RGB位图,而不是类似于32bit RGBA的文件。
尝试使用32而不是24的位数,BMP像素中可能有未使用的字节:
bmpInfo.bmiHeader.biBitCount = 32;https://stackoverflow.com/questions/16604357
复制相似问题