我有我需要转换为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。 这是什么问题,如何解决?
我发现如何做到这一点。 我们需要使用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;
}
您的文章是非常有益的(答案),但是它没有工作对我来说,这里是微小调整的代码:
// 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);
这里的其他答案是非常有益的,但是我最终能够只用1行来实现它。
HBITMAP hBm = CreateBitmap(width,height,1,32,pixels); // 1 plane, 32 bits
希望它可以是任何未来的读者有用。
此外,还有CreateCompatibleBitmap
和SetDIBits
柜面你需要使用一个设备上下文。
在MSDN的CreateDIBitmap
它指出:
调用带有fdwInit CreateDIBitmap作为CBM_INIT是相当于调用CreateCompatibleBitmap函数在设备的格式来创建一个DDB,然后调用SetDIBits起到将DIB位到DDB平移。