How to convert an Array of pixels to HBITMAP
Asked Answered
G

3

13

I have an array of pixels which I need to convert to HBITMAP in order to display it in a window. I tried to use CreateDIBitmap() but I don't have the BMP headers. I tried to construct them manually according to MSDN documentation but this didn't work.

Here how my code looks

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);

Now I get a non NULL hBitmap which is good but it shows always black image as if it doesn't point to the array of pixels. I checked it using the code

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

And indeed qB.bmBits is NULL. What is the problem and how to fix it?

Gurolinick answered 10/4, 2013 at 15:46 Comment(3)
I don't think CreateDIBitmap supports grayscale images. You could create a palette of 256 shades of grey or you could convert your image to RGB triples.Bartizan
OK, suppose I do: char pixels[160*120*3]; than I have RGB image.Gurolinick
I set bmih.biBitCount = 24; but I still doesn't workGurolinick
G
9

I found how to do it. We need to use CreateDIBSection() instead of CreateDIBitmap() So here is the working code

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));

For grey level images, copy the pixels to DIB in a loop instead of with 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;
}  
Gurolinick answered 11/4, 2013 at 7:14 Comment(2)
Note: You might need to pad the width to multiple of 4 bytes (32bits integer) instead of 16bits WORD like I did in the example.Gurolinick
I don't think void* bits = (void*)&(pixels[0]); makes any sense here. Should just make it: void* bits = NULL;.Fratricide
U
5

Your post was very helpful (the answer) however it didn't work for me, here is the code with small corrections:

    // 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);
Uniformity answered 26/1, 2016 at 10:38 Comment(1)
You forgot to close the for loop at line 5-6 with a }Sander
S
1

The other answers here are very helpful, but i ended up being able to achieve it with just 1 line.

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

Hope it may be useful to any future readers.


Also, there's CreateCompatibleBitmap and SetDIBits incase you need to use a device context.

At MSDN for CreateDIBitmap it states:

Calling CreateDIBitmap with fdwInit as CBM_INIT is equivalent to calling the CreateCompatibleBitmap function to create a DDB in the format of the device and then calling the SetDIBits function to translate the DIB bits to the DDB.

Spontoon answered 28/4, 2019 at 22:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.