I have a void pointer to data that was read from a *.bmp file. The bmp file no longer exists (there is now one file that contains hundreds of these bitmap file datasets). How can I initialize an MFC CBitmap
using this data?
I see the CBitmap::Create*
functions (e.g., CreateBitmap()
, CreateCompatibleBitmap()
, etc), but they require that I know the bitmap's height and width, can get access to the data bits, etc. I can write the data to disk and then use ::LoadImage()
and CBitmap::Attach()
to load the bitmap, but I want to do this in memory to improve performance.
Thanks!
UPDATE (#2):
Here is my code, as suggested and simplified by Constantine Georgiou's comments and post (thank you!). CBitmap::CreateBitmap() no longer fails, but the bitmap displays as black.
// Bitmap File Header
LPBITMAPFILEHEADER pFileHdr = (LPBITMAPFILEHEADER)pFileData;
// Bitmap Info Header
LPBITMAPINFOHEADER pBmpHdr = (LPBITMAPINFOHEADER)((PCHAR)pFileData + sizeof(BITMAPFILEHEADER));
// Image Data
LPVOID lpBits = (LPVOID)((PCHAR)pFileData + pFileHdr->bfOffBits);
if(!bitmap.CreateBitmap(pBmpHdr->biWidth, pBmpHdr->biHeight, pBmpHdr->biPlanes, pBmpHdr->biBitCount, lpBits))
bool bummer = true;
Here is code that writes the same data to a file and then loads the bitmap using ::LoadImage(). This works.
CFile file;
if(file.Open(sFilename, CFile::modeCreate | CFile::modeReadWrite))
{
file.Write(pFileData, dwFileBytes);
file.Close();
HBITMAP hBitmap = (HBITMAP)::LoadImage(NULL, sFilename, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
if(hBitmap)
{
if(!bitmap.Attach(hBitmap))
bool ahHeck = true;
}
}
Here are trace messages regarding the above.
Inspecting the file data in memory before creating the image:
dwFileBytes = 3128.
- BMP HEADER:
biSize = 40
biWidth = 32
biHeight = 32
biPlans = 1
biBitCount = 24
biCompression = 0
biSizeImage = 3074
Inspecting BITMAP after calling CreateBitmap(32, 32, 1, 24, lpBits):
bmType = 0
bmWidth = 32
bmHeight = 32
bmPlanes= 1
bmWidthBytes = 96
bmBitsPixel = 24
bmBits = 0
(This bitmap displays as black.)
Inspecting BITMAP after writing to file and calling LoadImage():
bmType = 0
bmWidth = 32
bmHeight = 32
bmPlanes= 1
bmWidthBytes = 128
bmBitsPixel = 32
bmBits = 0
(This bitmap displays correctly.)
I realize I'm getting into the weeds with details. Apologies! I'm stumped.