How can I create a colour btimap ?
-
Hi everyone. Recently I've began to study WTL programming and I 've got a question. Please explain me how can I create a colour bitmap from array of bits? Not colour bitmap I create like this: HBITMAP btmp; LPBITMAPINFO pbi; LPBITMAPINFOHEADER pbmph; bool CDisplay::initBmp(byte* Img) { if(pbi != NULL) delete [] pbi; if(btmp != NULL) DeleteObject(btmp); int headerSize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256; try { pbi = (LPBITMAPINFO)new char [headerSize]; pbmph = &(pbi->bmiHeader); memset(pbmph, 0, headerSize); pbmph->biSize = sizeof(BITMAPINFOHEADER); pbmph->biPlanes = 1; pbmph->biCompression = BI_RGB; pbmph->biClrUsed = 256; pbmph->biClrImportant = 256; pbmph->biBitCount = 8; // fill RGBQUAD LPRGBQUAD pRGBQ = (LPRGBQUAD)((LPSTR)pbmph + pbmph->biSize); for( int i = 0; i < 256; i++ ) { pRGBQ[i].rgbBlue = pRGBQ[i].rgbGreen = pRGBQ[i].rgbRed = i; } } catch(...) { return false; } pbmph->biWidth = MaxX[0]; pbmph->biHeight = MaxY[0]; pbmph->biSizeImage = MaxX[0]*MaxY[0]; btmp = CreateDIBitmap(GetDC(), pbmph, CBM_INIT, Img, pbi, DIB_RGB_COLORS); return true; } How create colour bitmap? P.S.: Sorry for my english if I wrote with errors. Nulla dies sine linea !!!
-
Hi everyone. Recently I've began to study WTL programming and I 've got a question. Please explain me how can I create a colour bitmap from array of bits? Not colour bitmap I create like this: HBITMAP btmp; LPBITMAPINFO pbi; LPBITMAPINFOHEADER pbmph; bool CDisplay::initBmp(byte* Img) { if(pbi != NULL) delete [] pbi; if(btmp != NULL) DeleteObject(btmp); int headerSize = sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256; try { pbi = (LPBITMAPINFO)new char [headerSize]; pbmph = &(pbi->bmiHeader); memset(pbmph, 0, headerSize); pbmph->biSize = sizeof(BITMAPINFOHEADER); pbmph->biPlanes = 1; pbmph->biCompression = BI_RGB; pbmph->biClrUsed = 256; pbmph->biClrImportant = 256; pbmph->biBitCount = 8; // fill RGBQUAD LPRGBQUAD pRGBQ = (LPRGBQUAD)((LPSTR)pbmph + pbmph->biSize); for( int i = 0; i < 256; i++ ) { pRGBQ[i].rgbBlue = pRGBQ[i].rgbGreen = pRGBQ[i].rgbRed = i; } } catch(...) { return false; } pbmph->biWidth = MaxX[0]; pbmph->biHeight = MaxY[0]; pbmph->biSizeImage = MaxX[0]*MaxY[0]; btmp = CreateDIBitmap(GetDC(), pbmph, CBM_INIT, Img, pbi, DIB_RGB_COLORS); return true; } How create colour bitmap? P.S.: Sorry for my english if I wrote with errors. Nulla dies sine linea !!!
You might want to look at CreateDIBSection. This is similar to what you already have (it will return a HBITMAP handle and a pointer to the bits). You can directly modify the bits via that pointer and use the handle for selecting the object into DCs. If you simply want to create a 24-bit colour image, you don't need to create palette information in the BITMAPINFOHEADER (set those values to 0). Then simply write an RGB TRIPLE (Blue, Green, Red) for each pixel using the pointer. Remember you will need to pad each scanline to the DWORD boundary. If you want to write a 256-colour (paletted) image, you will need to provide a colour table. Check out MSDN documentation for that information. Another gotcha is that if you want the bitmap to be "top-down" i.e. pixel (0,0) is in the top-left corner, you must specify a NEGATIVE height value in the BITMAPINFOHEADER structure. Hope that helps
-
You might want to look at CreateDIBSection. This is similar to what you already have (it will return a HBITMAP handle and a pointer to the bits). You can directly modify the bits via that pointer and use the handle for selecting the object into DCs. If you simply want to create a 24-bit colour image, you don't need to create palette information in the BITMAPINFOHEADER (set those values to 0). Then simply write an RGB TRIPLE (Blue, Green, Red) for each pixel using the pointer. Remember you will need to pad each scanline to the DWORD boundary. If you want to write a 256-colour (paletted) image, you will need to provide a colour table. Check out MSDN documentation for that information. Another gotcha is that if you want the bitmap to be "top-down" i.e. pixel (0,0) is in the top-left corner, you must specify a NEGATIVE height value in the BITMAPINFOHEADER structure. Hope that helps
TNX, That helped me. I did it. Nulla dies sine linea !!!