如何将像素阵列转换为HBITMAP [英] How to convert an Array of pixels to HBITMAP
问题描述
我有一个像素数组,我需要转换为HBITMAP,以便在窗口中显示它。
我试图使用CreateDIBitmap(),但我没有BMP头。
我试图根据MSDN文档手动构建它们,但是这不工作。
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.
这里我的代码看起来
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是好的,但它总是显示黑色图像, t指向像素阵列。
我使用代码检查它
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));
确实qB.bmBits为NULL。
问题和如何解决?
And indeed qB.bmBits is NULL. What is the problem and how to fix it?
推荐答案
我找到了如何做。
我们需要使用 CreateDIBSection()
而不是 CreateDIBitmap()
工作代码
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));
对于灰度级图像,将像素复制到DIB,而不是使用memcpy()
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;
}
这篇关于如何将像素阵列转换为HBITMAP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!