Array [byte] in HBITMAP or CBitmap

I have an array of bytes (which I read through the stream directly from .bmp and then saved as a BLOB in the database) that I want to display as icons in a CImageList. So I want to somehow load my data into HBITMAP or CBitmap. I have done this so far, reading from a file:

hPic = (HBITMAP)LoadImage(NULL, strPath, IMAGE_BITMAP, dwWidth, dwHeight, LR_LOADFROMFILE | LR_VGACOLOR); ... CBitmap bitmap; bitmap.Attach(hPicRet); 

But obviously this only works for files, but not for byte arrays. How can I get the same result, but reading from an array of bytes?

Edit: Please note that my array does not contain only color information, but rather a complete file, as it is written to disk, including all the headers and metadata. It seems to me that giving up this information is a bad idea.

+8
c ++ mfc
source share
4 answers

Assuming you loaded the information into a BYTE array named bytes ....

 BITMAPFILEHEADER* bmfh; bmfh = (BITMAPFILEHEADER*)bytes; BITMAPINFOHEADER* bmih; bmih = (BITMAPINFOHEADER*)(bytes + sizeof(BITMAPFILEHEADER)); BITMAPINFO* bmi; bmi = (BITMAPINFO*)bmih; void* bits; bits = (void*)(bytes + bmfh->bfOffBits); HDC hdc = ::GetDC(NULL); HBITMAP hbmp = CreateDIBitmap(hdc, bmih, CBM_INIT, bits, bmi, DIB_RGB_COLORS) ; ::ReleaseDC(NULL, hdc); 

It's a bit messy and might use a healthy dose of error checking, but the basic idea sounds.

+16
source share

The following sample may help you.

 BITMAPINFO bmInfo; BITMAPINFOHEADER &bmInfohdr = (BITMAPINFOHEADER)bmInfo.bmiHeader; bmInfohdr.biSize = 40 + 255; //I think it not of use bmInfohdr.biWidth = x; bmInfohdr.biHeight = y; bmInfohdr.biPlanes=1; bmInfohdr.biBitCount=8; bmInfohdr.biCompression=0; bmInfohdr.biSizeImage=0; bmInfohdr.biXPelsPerMeter = 0; bmInfohdr.biYPelsPerMeter = 0; bmInfohdr.biClrUsed = 0; bmInfohdr.biClrImportant = 0; // should I allocate memory further than the // bmColors[1]?? anyway the compiler gives an // error for type mismatch! //bmInfo.bmiColors = (RGBQUAD *) malloc(sizeof(RGBQUAD) * 256); // here I define the 256 graylevel palette for (int i=0; i<256; i++) { bmInfo.bmiColors[i].rgbRed = i; bmInfo.bmiColors[i].rgbGreen = i; bmInfo.bmiColors[i].rgbBlue = i; } BYTE *matrix; matrix = (BYTE*)malloc(size*sizeof(BYTE)); // here I put the BYTE values of the pixels CDC *pdcDest = this->GetDC(); HBITMAP hBmp = CreateDIBitmap( pdcDest->m_hDC, &bmInfohdr, CBM_INIT, matrix, &bmInfo, DIB_RGB_COLORS); m_bmpBitmap.Attach( hBmp ); 
+2
source share

Something like this worked for me:

 int bitmap[WX*WY]; // truecolor bitmap data BITMAPINFO bm = { sizeof(BITMAPINFOHEADER), WX, WY, 1, 32, BI_RGB, 0, 0, 0, 0, 0 }; HBITMAP bmp = CreateDIBSection( GetDC(win), &bm, DIB_RGB_COLORS, (void**)&bitmap, 0,0 ); 

(This is specially configured for 32-bit colors, but you can specify any kind).

0
source share

Ok, here is a complete example: http://nishi.dreamhosters.com/u/so_bmp_v0.zip

 #include <stdio.h> #include <windows.h> #pragma comment(lib,"gdi32.lib") #pragma comment(lib,"user32.lib") char buf[1<<22]; int main( int argc, char **argv ) { FILE* f = fopen( "winnt.bmp", "rb" ); if( f==0 ) return 1; fread( buf, 1,sizeof(buf), f ); fclose(f); BITMAPFILEHEADER& bfh = (BITMAPFILEHEADER&)buf[0]; BITMAPINFO& bi = (BITMAPINFO&)buf[sizeof(BITMAPFILEHEADER)]; BITMAPINFOHEADER& bih = bi.bmiHeader; char* bitmap = &buf[bfh.bfOffBits]; int WX=1024, WY=512; // window width/height int SX=bih.biWidth, SY=bih.biHeight; HWND win = CreateWindow( "STATIC", "Bitmap test", 0x90C0, 0,0, WX,WY, 0,0, GetModuleHandle(0), 0 ); MSG msg; PAINTSTRUCT ps; HDC DC = GetDC(win); // window DC HBITMAP dib = CreateDIBitmap( DC, &bih, CBM_INIT, bitmap, &bi, DIB_RGB_COLORS ); HDC dibDC = CreateCompatibleDC( DC ); SelectObject( dibDC, dib ); ShowWindow( win, SW_SHOWNOACTIVATE ); SetFocus( win ); while( GetMessage(&msg,win,0,0) ) { int m = msg.message; if( m==WM_PAINT ) { DC = BeginPaint( win, &ps ); StretchBlt( DC, 0,0,WX,WY, dibDC,0,0,SX,SY, SRCCOPY ); EndPaint( win, &ps ); } else if( (m==WM_KEYDOWN) || (m==WM_SYSKEYDOWN) ) { break; } else { DispatchMessage(&msg); } } return 0; } 
0
source share

All Articles