| CodeGuru Home | VC++ / MFC / C++ | .NET / C# | Visual Basic | Newsletters | VB Forums | Developer.com |
|
|||||||
| Graphics Programming Discussion graphics programming using C++. Valid topics include OpenGL, DirectX, GDI/GDI+, Aero, and more. |
![]() |
|
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
32 bit DIB from 24 bit bitmap
Hello, I have a Win32 GDI question...
I need a way to make a 32 bit DIB from a 24 bit bitmap. I can load the bitmap with LoadImage but how to make it a 32 bit DIB? Any help would be greatly appreciated... |
|
#2
|
|||
|
|||
|
Re: 32 bit DIB from 24 bit bitmap
When your image is loaded, then use GetDIBits() with the argument containing the BITMAPINFO structure indicating you want a 32-bit bitmap, i.e. with bi.bmiHeader.biBitCount = 32:
Code:
BITMAPINFO bi;
...
bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
bi.bmiHeader.biWidth = picture_width;
bi.bmiHeader.biHeight = - picture_height;
bi.bmiHeader.biPlanes = 1;
bi.bmiHeader.biBitCount = 32;
bi.bmiHeader.biCompression = BI_RGB;
bi.bmiHeader.biSizeImage = bi.bmiHeader.biWidth * 4 * bi.bmiHeader.biHeight * -1 * sizeof(unsigned char);
bi.bmiHeader.biClrUsed = 0;
bi.bmiHeader.biClrImportant = 0;
...
if (!GetDIBits(hdc_mem, hbm, 0, picture_height, DIBits, &bi, DIB_RGB_COLORS)) {
...
|
|
#3
|
||||
|
||||
|
Re: 32 bit DIB from 24 bit bitmap
Ok, i have come up with a simple solution.
This is how to create an offscreen buffer (DIB32) and load a 24bit texture and make it DIB32 compatible. It seems i dont need to set the bmiHeader.biSizeImage value? Just zero it? Code:
// First we need a device context that is compatible
// with the desktop settings. (1024x768x32 on my PC)
HDC BufferDC = CreateCompatibleDC(NULL);
// Now we describle a format for the offscreen buffer.
BITMAPINFO BufferInfo;
ZeroMemory(&BufferInfo,sizeof(BITMAPINFO));
BufferInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
BufferInfo.bmiHeader.biWidth = GetSystemMetrics(SM_CXSCREEN);
BufferInfo.bmiHeader.biHeight = GetSystemMetrics(SM_CYSCREEN);
BufferInfo.bmiHeader.biPlanes = 1;
BufferInfo.bmiHeader.biBitCount = 32;
BufferInfo.bmiHeader.biCompression = BI_RGB;
// Create a device independant bitmap for the offscreen buffer.
HBITMAP BufferBitmap = CreateDIBSection(
BufferDC,
&BufferInfo,
DIB_RGB_COLORS,
(void**)&g_Buffer.first, // Store pointer to first pixel in this value.
NULL,0); // No file mapping crap.
// Describe the texture format.
// Same as buffer format but with hardwired size.
BITMAPINFO TextureInfo = BufferInfo;
TextureInfo.bmiHeader.biWidth = 100;
TextureInfo.bmiHeader.biHeight = 100;
// Create device independant bitmap for texture.
HBITMAP TextureBitmap = CreateDIBSection(
BufferDC,
&TextureInfo,
DIB_RGB_COLORS,
(void**)&g_Texture.first, // Store pointer to first pixel in this value.
NULL,0); // No file mapping crap.
// Load the 24bit image from file.
HBITMAP hImage = (HBITMAP)LoadImage(
NULL, // No HINSTANCE since its not a resource.
"Image.bmp",
IMAGE_BITMAP,
TextureInfo.bmiHeader.biWidth,
TextureInfo.bmiHeader.biHeight,
LR_LOADFROMFILE);
// Transfer the bitmap into the texture DIB.
GetDIBits(
BufferDC,
hImage,
0, // First scan line.
TextureInfo.bmiHeader.biHeight, // Number of scan lines.
g_Texture.first, // Pointer to first pixel.
&TextureInfo, // Address of BITMAPINFO with desired specs.
DIB_RGB_COLORS);
// Were done with the inage so...
DeleteObject(hImage);
// Select the buffer for rendering to.
HBITMAP OldBitmap = (HBITMAP)SelectObject(BufferDC,BufferBitmap);
// Dont forget to clean up later :)
|
![]() |
| Bookmarks |
|
||||||
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|