c++ - GetDIBits() 返回错误的 BGR 值 :

标签 c++ winapi getdibits

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)。代码可能有什么问题?

最佳答案

看起来您的值发生了变化,可能是因为您缺少一个字节。

您应该检查 BMP 文件实际上是一个 24bit RGB 位图,而不是像 32bit RGBA 这样的东西。

尝试使用 32 而不是 24,您的 BMP 像素中可能有一个未使用的字节:

bmpInfo.bmiHeader.biBitCount = 32;

关于c++ - GetDIBits() 返回错误的 BGR 值 :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16604357/

相关文章:

c++ - WINAPI C++ VS 2008 与 VS 2010

c - 使用 Writefile 函数将字符串放入 C 中的非事件屏幕缓冲区

c++ - 使用 GetDIBits 访问图像的像素颜色

gdi - GetDIBits 中的 HDC 是什么?

c++ - 重新映射模板参数结构

c++ - 没有合适的用户定义的从 utility::string_t 到 std::string 的转换

.net - 有没有办法/工具来显示系统中所有的内存映射文件?

C++/Win32 : How to get the alpha channel from an HBITMAP?

c++ - g++ : Is there a way to access compile flags inside the code that is being compiled?

c++ - 如何在 mfc 中为多线程应用程序创建公共(public)日志文件?