10

我有一个像素数组,我需要将其转换为 HBITMAP 以便在窗口中显示它。我尝试使用 CreateDIBitmap() 但我没有 BMP 标头。我尝试根据 MSDN 文档手动构建它们,但这不起作用。

这是我的代码的外观

HBITMAP hBitmap
char pixels[160*120]; // White grayscale image of size 160x120
memset(pixels,255,sizeof(pixels));

BITMAPINFOHEADER bmih;
bmih.biSize     = sizeof(BITMAPINFOHEADER);
bmih.biWidth    = 160;
bmih.biHeight   = -120;
bmih.biPlanes   = 1;
bmih.biBitCount = 8;
bmih.biCompression  = BI_RGB ;
bmih.biSizeImage    = 0;
bmih.biXPelsPerMeter    =   10;
bmih.biYPelsPerMeter    =   10;
bmih.biClrUsed  =0;
bmih.biClrImportant =0;

BITMAPINFO dbmi;
dbmi.bmiHeader = bmih;
dbmi.bmiColors->rgbBlue = 0;
dbmi.bmiColors->rgbGreen = 0;
dbmi.bmiColors->rgbRed = 0;
dbmi.bmiColors->rgbReserved = 0;
void* bits = (void*)&(pixels[0]); 
hBitmap = CreateDIBitmap(localDC, &bmih, CBM_INIT, qB.bmBits, &dbmi, DIB_RGB_COLORS);

现在我得到一个非 NULL hBitmap 这很好,但它总是显示黑色图像,好像它没有指向像素数组。我使用代码检查了它

BITMAP qB;
GetObject(reinterpret_cast<HGDIOBJ>(hBitmap),sizeof(BITMAP),reinterpret_cast<LPVOID>(&qB));

实际上 qB.bmBits 是 NULL。有什么问题以及如何解决?

4

3 回答 3

7

我找到了怎么做。我们需要使用CreateDIBSection()而不是CreateDIBitmap() 所以这里是工作代码

HBITMAP hBitmap = NULL;
    unsigned char pixels[160*120*3]; 
    for (int i=0; i<160*120*3; i++){
        pixels[i] = (i%4==1)*255;        // An BGR (not RGB) 160x120 image.
    }
BITMAPINFOHEADER bmih;
bmih.biSize     = sizeof(BITMAPINFOHEADER);
bmih.biWidth    = 160;
bmih.biHeight   = -120;
bmih.biPlanes   = 1;
bmih.biBitCount = 24;
bmih.biCompression  = BI_RGB ;
bmih.biSizeImage    = 0;
bmih.biXPelsPerMeter    =   10;
bmih.biYPelsPerMeter    =   10;
bmih.biClrUsed    =0;
bmih.biClrImportant =0;

BITMAPINFO dbmi;
ZeroMemory(&dbmi, sizeof(dbmi));  
dbmi.bmiHeader = bmih;
dbmi.bmiColors->rgbBlue = 0;
dbmi.bmiColors->rgbGreen = 0;
dbmi.bmiColors->rgbRed = 0;
dbmi.bmiColors->rgbReserved = 0;
void* bits = (void*)&(pixels[0]); 

// Create DIB
hBitmap = CreateDIBSection(localDC, &dbmi, DIB_RGB_COLORS, &bits, NULL, 0);
if (hBitmap == NULL) {
    ::MessageBox(NULL, __T("Could not load the desired image image"), __T("Error"), MB_OK);
    return;
}
// copy pixels into DIB.
memcpy(bits,pixels,sizeof(pixels));

对于灰度图像,将像素循环复制到 DIB,而不是使用 memcpy()

#define INTENSITY unsigned char

INTENSITY* dest = (INTENSITY*)bits;
const INTENSITY* src  = .. Put your char array of pixels;
for (int j=0; j<imageWidth; j++){
    for (int i=0; i<imageHeight; i++, src++){
        *dest++ = *src;
        *dest++ = *src;
        *dest++ = *src;
    }
    // Padd the line to round WORD.
    if (imageWidth%2)
        *dest++ = 0;
}  
于 2013-04-11T07:14:47.267 回答
4

您的帖子非常有帮助(答案)但是它对我不起作用,这是带有小修正的代码:

    // creating input

    unsigned char pixels[160*120*3]; 
    for (int i=0; i<160*120*3; i++)
        pixels[i] = (i%4==1)*255;        // An BGR (not RGB) 160x120 image.

    // at this point we have some input

    BITMAPINFOHEADER bmih;
    bmih.biSize     = sizeof(BITMAPINFOHEADER);
    bmih.biWidth    = 160;
    bmih.biHeight   = -120;
    bmih.biPlanes   = 1;
    bmih.biBitCount = 24;
    bmih.biCompression  = BI_RGB ;
    bmih.biSizeImage    = 0;
    bmih.biXPelsPerMeter    =   10;
    bmih.biYPelsPerMeter    =   10;
    bmih.biClrUsed    =0;
    bmih.biClrImportant =0;

    BITMAPINFO dbmi;
    ZeroMemory(&dbmi, sizeof(dbmi));  
    dbmi.bmiHeader = bmih;
    dbmi.bmiColors->rgbBlue = 0;
    dbmi.bmiColors->rgbGreen = 0;
    dbmi.bmiColors->rgbRed = 0;
    dbmi.bmiColors->rgbReserved = 0;

    HDC hdc = ::GetDC(NULL);

    HBITMAP hbmp = CreateDIBitmap(hdc, &bmih, CBM_INIT, pixels, &dbmi, DIB_RGB_COLORS);
    if (hbmp == NULL) {
        ::MessageBox(NULL, L"Could not load the desired image image", L"Error", MB_OK);
        return;
    }

    ::ReleaseDC(NULL, hdc);

    // a little test if everything is OK
    OpenClipboard(NULL);
    EmptyClipboard();
    SetClipboardData(CF_BITMAP, hbmp);
    CloseClipboard();

    // cleanup
    DeleteObject(hbmp);
于 2016-01-26T10:38:36.770 回答
2

这里的其他答案非常有帮助,但我最终只用 1 行就可以实现它。

HBITMAP hBm = CreateBitmap(width,height,1,32,pixels); // 1 plane, 32 bits

希望对以后的读者有用。


此外,CreateCompatibleBitmapSetDIBits还需要使用设备上下文。

在 MSDN 中,CreateDIBitmap它指出:

以 fdwInit 为 CBM_INIT 调用 CreateDIBitmap 相当于调用 CreateCompatibleBitmap 函数创建设备格式的 DDB,然后调用 SetDIBits 函数将 DIB 位转换为 DDB。

于 2019-04-28T22:37:32.873 回答