After succesfull call to CreateDIBSection(), call GetDIBits() 0 in lpvBits to get biSizeImage
or use GetObject() with DIBSECTION.
Example in C:#define WIN32_LEAN_AND_MEAN
#include <windows.h>
int __cdecl WinMainCRTStartup(void)
{
BITMAPINFO bmi = {{sizeof(BITMAPINFOHEADER),200,200,1,32},};
RGBQUAD *prgbBits;
HDC hdcWin = GetDC(0);
HBITMAP hbm = CreateDIBSection(hdcWin, (BITMAPINFO *)&bmi,
DIB_RGB_COLORS, (void *)(&prgbBits), NULL, 0);
if (hbm) {
BITMAPFILEHEADER bfh = {0x4D42};
DIBSECTION ds;
GetObject(hbm, sizeof(DIBSECTION), &ds);
bfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bfh.bfSize = bfh.bfOffBits + ds.dsBmih.biSizeImage;
HANDLE hFile = CreateFile("TestDIB3.bmp", GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwWritten = 0;
WriteFile( hFile, &bfh, sizeof(bfh), &dwWritten , NULL );
WriteFile( hFile, &ds.dsBmih, sizeof(BITMAPINFOHEADER), &dwWritten, NULL );
WriteFile( hFile, ds.dsBm.bmBits, ds.dsBmih.biSizeImage, &dwWritten, NULL );
CloseHandle( hFile );
DeleteObject(hbm);
}
ReleaseDC(0, hdcWin);
ExitProcess(0);
}