How do I create a bitmap using the BITMAPV5HEADER header? - c++

This is my current code, but it causes an error when casting to BITMAPINFOHEADER:
/* Create the bitmap */
BITMAPINFO bmpinfo;
ZeroMemory(&bmpinfo, sizeof(bmpinfo));
BITMAPV5HEADER bmpheader;
ZeroMemory(&bmpheader, sizeof(bmpheader));
bmpheader.bV5Size = sizeof(BITMAPV5HEADER);
bmpheader.bV5Width = width;
bmpheader.bV5Height = height;
bmpheader.bV5Planes = 1;
bmpheader.bV5BitCount = 32;
bmpheader.bV5Compression = BI_BITFIELDS;
bmpheader.bV5SizeImage = width*height*4;
bmpheader.bV5RedMask = 0x00FF0000;
bmpheader.bV5GreenMask = 0x0000FF00;
bmpheader.bV5BlueMask = 0x000000FF;
bmpheader.bV5AlphaMask = 0xFF000000;
bmpheader.bV5CSType = 0x57696e20; // LCS_WINDOWS_COLOR_SPACE
bmpheader.bV5Intent = LCS_GM_BUSINESS;
bmpinfo.bmiHeader = reinterpret_cast<BITMAPINFOHEADER>(bmpheader);
void* converted = NULL;
HDC screen = GetDC(NULL);
HBITMAP result = CreateDIBSection(screen, &bmpinfo, DIB_RGB_COLORS, &converted, NULL, 0);
ReleaseDC(NULL, screen);
// Image data filled here
How will I be able to do this successfully?

BITMAPINFO isn't a true structure, it's more just documentation about how the color map follows the header. Just pass your BITMAPV5HEADER directly to CreateDIBSection:
HBITMAP result = CreateDIBSection(screen, reinterpret_cast<BITMAPINFO *>(&bmpheader), DIB_RGB_COLORS, &converted, NULL, 0);

Related

How do I get the byte representation of a BITMAPFILEHEADER and BITMAPINFOHEADER?

I'm trying to take a screenshot of my computer screen using code, and then store the BYTEs of that image into an array rather than save the image right away using the following function:
void WINAPI CaptureScreenIntoByteArray(
BYTE*& screen_bytes,
DWORD& screen_bytes_size)
{
BITMAPFILEHEADER bfHeader;
BITMAPINFOHEADER biHeader;
BITMAPINFO bInfo;
HGDIOBJ hTempBitmap;
HBITMAP hBitmap;
BITMAP bAllDesktops;
HDC hDC, hMemDC;
LONG lWidth, lHeight;
BYTE* sb = NULL;
ZeroMemory(&bfHeader, sizeof(BITMAPFILEHEADER));
ZeroMemory(&biHeader, sizeof(BITMAPFILEHEADER));
ZeroMemory(&bInfo, sizeof(BITMAPINFO));
ZeroMemory(&bAllDesktops, sizeof(BITMAP));
hDC = GetDC(NULL);
hTempBitmap = GetCurrentObject(hDC, OBJ_BITMAP);
GetObjectW(hTempBitmap, sizeof(BITMAP), &bAllDesktops);
lWidth = bAllDesktops.bmWidth;
lHeight = bAllDesktops.bmHeight;
DeleteObject(hTempBitmap);
bfHeader.bfType = (WORD)('B' | ('M' << 8));
bfHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
biHeader.biSize = sizeof(BITMAPINFOHEADER);
biHeader.biBitCount = 24;
biHeader.biCompression = BI_RGB;
biHeader.biPlanes = 1;
biHeader.biWidth = lWidth;
biHeader.biHeight = lHeight;
bInfo.bmiHeader = biHeader;
screen_bytes_size = (((24 * lWidth + 31) & ~31) / 8) * lHeight;
hMemDC = CreateCompatibleDC(hDC);
hBitmap = CreateDIBSection(hDC, &bInfo, DIB_RGB_COLORS, (VOID**)&sb, NULL, 0);
SelectObject(hMemDC, hBitmap);
int x = GetSystemMetrics(SM_XVIRTUALSCREEN);
int y = GetSystemMetrics(SM_YVIRTUALSCREEN);
BitBlt(hMemDC, 0, 0, lWidth, lHeight, hDC, x, y, SRCCOPY);
// Need to also copy bfHeader & biHeader bytes somehow...
screen_bytes = new BYTE[screen_bytes_size];
std::copy(sb, sb + screen_bytes_size, screen_bytes);
DeleteDC(hMemDC);
ReleaseDC(NULL, hDC);
DeleteObject(hBitmap);
}
My specific issue is that I need to have the bfHeader and biHeader saved into the byte array also (so that later on I can save the image as a file)... So this is the problem area...
// Need to also copy bfHeader & biHeader bytes somehow...
screen_bytes = new BYTE[screen_bytes_size];
std::copy(sb, sb + screen_bytes_size, screen_bytes);
How can I also copy the bfHeader and biHeader bytes into the array?
If needed, this is how you call the function...
BYTE* screen_bytes = NULL;
DWORD screen_bytes_size = 0;
CaptureScreenIntoByteArray(screen_bytes, screen_bytes_size);
You need to make the array big enough for the two structures as well.
screen_bytes = new BYTE[sizeof(BITMAPFILEHEADER) + sizeof (BITMAPINFOHEADER) + screen_bytes_size];
Then you can copy the two structures to the front of the array:
std::copy(&bfHeader, &bfHeader + 1, (BITMAPFILEHEADER*)screen_bytes);
std::copy(&biHeader, &biHeader + 1, (BITMAPINFOHEADER*)(screen_bytes + sizeof(BITMAPFILEHEADER)));
And copy the image data after the two structures.
std::copy(sb, sb + screen_bytes_size, screen_bytes + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER));

CreateDIBSection() failed - how to diagnose

I have a call to CreateDIBSection() which is returning 0. The documentation says that this means that "One or more of the input parameters is invalid" but there is no indication of which parameter is invalid.
I had a look that this SO question in which the question ends with "after CreateDIBSection I found the error code is 8, no enough resource" - this confused me - there is no mention of any error codes in the documentation. How did the the poster get this extra information?
EDIT: As requested, here is my code - I must apologise it is not complete - it is part of a huge program, and making a minimal winapi program is not trivial.
HDC hdcTemp;
BYTE* bitPointer;
hdcTemp = CreateCompatibleDC(hdc_desktop);
my_printf("GetDeviceCaps(hdcTemp,BITSPIXEL) = %d\n",GetDeviceCaps(hdcTemp,BITSPIXEL)); // this prints "32"
static BITMAPINFO bitmap;
bitmap.bmiHeader.biSize = sizeof(bitmap.bmiHeader);
bitmap.bmiHeader.biWidth = 280;
bitmap.bmiHeader.biHeight = height_to_check;
bitmap.bmiHeader.biPlanes = 1;
bitmap.bmiHeader.biBitCount = 32;
bitmap.bmiHeader.biCompression = BI_RGB;
bitmap.bmiHeader.biSizeImage = 280 * 4 * height_to_check;
bitmap.bmiHeader.biClrUsed = 0;
bitmap.bmiHeader.biClrImportant = 0;
HBITMAP hBitmap2 = CreateDIBSection(hdcTemp, &bitmap, DIB_RGB_COLORS, (void**)(&bitPointer), NULL, NULL);
change to
BYTE* bitPointer;
HDC hdcScreen = GetDC(NULL);
static BITMAPINFO bitmap;
bitmap.bmiHeader.biSize = sizeof(bitmap.bmiHeader);
bitmap.bmiHeader.biWidth = 280;
bitmap.bmiHeader.biHeight = height_to_check;
bitmap.bmiHeader.biPlanes = 1;
bitmap.bmiHeader.biBitCount = 32;
bitmap.bmiHeader.biCompression = BI_RGB;
bitmap.bmiHeader.biSizeImage = 280 * 4 * height_to_check;
bitmap.bmiHeader.biClrUsed = 0;
bitmap.bmiHeader.biClrImportant = 0;
HBITMAP hBitmap2 = CreateDIBSection(hdcScreen, &bitmap, DIB_RGB_COLORS, (void**)(&bitPointer), NULL, NULL);
ReleaseDC(NULL, hdcScreen);
i.e. don't pass an HDC returned from CreateCompatibleDC to CreateDIBSection; use the device context of the screen.
Calling CreateCompatibleDC creates the device context with a 1x1 monochrome bitmap associated with it no matter what HDC you passed in so when you create a bitmap or DIB section compatible to that HDC Win32 tries to be compatible with the monochrome bitmap which you dont want.
At present, MSDN points out that "one or more input parameters are invalid" is reasonable. Tests show that if the bitmap size is too large or the input value is invalid, such as zero, NULL will be returned.

C++ GDI+ SelectPalette

I am playing with GDI+. Trying to use
pDC->SelectPalette(CPalette::FromHandle(hLogPal), FALSE);
pDC->RealizePalette();
instead of
memcpy(newBitmapInfo + sizeof(BITMAPINFO), rgbquad, palettesize);
But it seem that with it's working with memcpy(newBitmapInfo + sizeof(BITMAPINFO), rgbquad, palettesize); but with SelectPalette only black screen.
I thought that information about color can be used from bitmapinfo or from pallet.
All code:
void ConvertTo8BitImage(BYTE** pBitmapInfo, BYTE** imageData)
{
Gdiplus::GdiplusStartupInput tmp;
ULONG_PTR token;
Gdiplus::GdiplusStartup(&token, &tmp, NULL);
Gdiplus::Bitmap *source = Gdiplus::Bitmap::FromFile(L"D:/TestImage.bmp");
Gdiplus::Bitmap *destination = source->Clone(0, 0, source->GetWidth(), source->GetHeight(),
PixelFormat8bppIndexed);
int width = source->GetWidth();
int height = source->GetHeight();
HBITMAP hBitmap;
Gdiplus::Color color;
destination->GetHBITMAP(color, &hBitmap);
int palettesize = 256 * sizeof(RGBQUAD);
CLSID clsid_bmp;
CLSIDFromString(L"{557cf400-1a04-11d3-9a73-0000f81ef32e}", &clsid_bmp);
*pBitmapInfo = new BYTE[(sizeof(BITMAPINFO) + palettesize)];
BITMAPINFO* ptr = (BITMAPINFO*)*pBitmapInfo;
ptr->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
ptr->bmiHeader.biWidth = width;
ptr->bmiHeader.biHeight = height;
ptr->bmiHeader.biPlanes = 1;
ptr->bmiHeader.biBitCount = 8;
ptr->bmiHeader.biCompression = BI_RGB;
ptr->bmiColors[0].rgbRed = 0;
DWORD size = ((width * 8 + 31) / 32) * 4 * height;
*imageData = new BYTE[size];
HDC hdc = GetDC(0);
GetDIBits(hdc, hBitmap, 0, height, *imageData, (BITMAPINFO*)*pBitmapInfo, DIB_PAL_COLORS);
ReleaseDC(0, hdc);
Gdiplus::GdiplusShutdown(token);
}
void CMFCApplicationColorsView::OnDraw(CDC* pDC)
{
CMFCApplicationColorsDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
BYTE *bitmapInfo = NULL;
BYTE *imageData = NULL;
ConvertTo8BitImage(&bitmapInfo, &imageData);
int palettesize = 256 * sizeof(RGBQUAD);
BYTE *newBitmapInfo = new BYTE[(sizeof(BITMAPINFO) + palettesize)];
ZeroMemory(newBitmapInfo, (sizeof(BITMAPINFO) + palettesize));
BITMAPINFO *ptr = (BITMAPINFO*)newBitmapInfo;
ptr->bmiHeader.biBitCount = ((BITMAPINFO*)bitmapInfo)->bmiHeader.biBitCount;
ptr->bmiHeader.biClrImportant = ((BITMAPINFO*)bitmapInfo)->bmiHeader.biClrImportant;
ptr->bmiHeader.biClrUsed = ((BITMAPINFO*)bitmapInfo)->bmiHeader.biClrUsed;
ptr->bmiHeader.biCompression = ((BITMAPINFO*)bitmapInfo)->bmiHeader.biCompression;
ptr->bmiHeader.biHeight = ((BITMAPINFO*)bitmapInfo)->bmiHeader.biHeight;
ptr->bmiHeader.biPlanes = ((BITMAPINFO*)bitmapInfo)->bmiHeader.biPlanes;
ptr->bmiHeader.biSize = ((BITMAPINFO*)bitmapInfo)->bmiHeader.biSize;
ptr->bmiHeader.biSizeImage = ((BITMAPINFO*)bitmapInfo)->bmiHeader.biSizeImage;
ptr->bmiHeader.biWidth = ((BITMAPINFO*)bitmapInfo)->bmiHeader.biWidth;
ptr->bmiHeader.biXPelsPerMeter = ((BITMAPINFO*)bitmapInfo)->bmiHeader.biXPelsPerMeter;
ptr->bmiHeader.biYPelsPerMeter = ((BITMAPINFO*)bitmapInfo)->bmiHeader.biYPelsPerMeter;
ptr->bmiColors[0] = ((BITMAPINFO*)bitmapInfo)->bmiColors[0];
RGBQUAD rgbquad[256];
memcpy(rgbquad, bitmapInfo + sizeof(BITMAPINFO), palettesize);
//memcpy(newBitmapInfo + sizeof(BITMAPINFO), rgbquad, palettesize);
NPLOGPALETTE pPal = (NPLOGPALETTE)LocalAlloc(LMEM_FIXED,
(sizeof(LOGPALETTE) +
(sizeof(PALETTEENTRY) * (palettesize))));
pPal->palVersion = 0x300;
pPal->palNumEntries = 256;
for (int i = 0; i < 256; i++)
{
pPal->palPalEntry[i].peRed = rgbquad[i].rgbRed;
pPal->palPalEntry[i].peGreen = rgbquad[i].rgbGreen;
pPal->palPalEntry[i].peBlue = rgbquad[i].rgbBlue;
pPal->palPalEntry[i].peFlags = 0;
}
HPALETTE hLogPal = CreatePalette((LPLOGPALETTE)pPal);
pDC->SelectPalette(CPalette::FromHandle(hLogPal), FALSE);
pDC->RealizePalette();
StretchDIBits(pDC->GetSafeHdc(), 0, 0, 1920, 1080, 0, 0, 1920, 1080,
imageData, ptr, DIB_PAL_COLORS, SRCCOPY);
delete[] bitmapInfo;
delete[] imageData;
}
HBITMAP hBitmap;
Gdiplus::Color color;
destination->GetHBITMAP(color, &hBitmap);
You did convert to 8-bit bitmap, however GetHBITMAP will return a bitmap handle compatible with your video card, which is probably 32-bit. GDI+ has already processed the palette and returned a bitmap handle which is turned back in to 32-bit. HBITMAP handle can be painted directly, for example using CreateCompatibleDC and BitBlt. So there is no need to obtaining the palette and passing it to GDI, and no need for 8-bit conversion in the first place.
If this is necessary for some reason, you can get the bits and palette from 32-bit bitmap, put that in 8-bit bitmap, and draw with StretchDIBits
The main issue in your code is that it should use DIB_RGB_COLORS flag for GetDIBits/StretchDIBits, because the device context is most likely 32-bit. There is no need for SelectPalette/RealizePalette either (unless it's 8-bit display from 30 years ago)
It makes more sense to get the bits directly from GDI+ using LockBits, and get the palette directly using GetPalette, as seen in the example below.
Aside, source and destination have to be deleted before exit.
void draw(HDC hdc)
{
Gdiplus::Bitmap *source = Gdiplus::Bitmap::FromFile(L"D:/TestImage.bmp");
if(!source)
return;
int width = source->GetWidth();
int height = source->GetHeight();
Gdiplus::Bitmap *destination = source->Clone(0, 0, width, height,
PixelFormat8bppIndexed);
//get bitmap bits from GDI+
Gdiplus::BitmapData data;
Gdiplus::Rect rect(0, 0, width, height);
destination->LockBits(&rect, Gdiplus::ImageLockModeRead,
destination->GetPixelFormat(), &data);
int bufsize = data.Stride * data.Height;
BYTE *buf = new BYTE[bufsize];
memcpy(buf, data.Scan0, bufsize);
destination->UnlockBits(&data);
//setup BITMAPINFO
int bmpinfo_size = sizeof(BITMAPINFO) + 256 * 4;
BITMAPINFO* bmpinfo = (BITMAPINFO*)new BYTE[bmpinfo_size];
memset(bmpinfo, 0, bmpinfo_size);
bmpinfo->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpinfo->bmiHeader.biWidth = width;
bmpinfo->bmiHeader.biHeight = -height;
bmpinfo->bmiHeader.biPlanes = 1;
bmpinfo->bmiHeader.biBitCount = 8;
bmpinfo->bmiHeader.biCompression = BI_RGB;
bmpinfo->bmiHeader.biSizeImage = bufsize;
//get palette from GDI+
int palsize = destination->GetPaletteSize();
Gdiplus::ColorPalette *palette = (Gdiplus::ColorPalette*)new BYTE[palsize];
destination->GetPalette(palette, palsize);
//set palette for BITMAPINFO
memset(&bmpinfo->bmiColors[0], 0, 256 * 4);
for(int i = 0; i < palette->Count; i++)
{
auto clr = Gdiplus::Color(palette->Entries[i]);
bmpinfo->bmiColors[i].rgbRed = clr.GetR();
bmpinfo->bmiColors[i].rgbGreen = clr.GetG();
bmpinfo->bmiColors[i].rgbBlue = clr.GetB();
bmpinfo->bmiColors[i].rgbReserved = 0;
}
StretchDIBits(hdc, 0, 0, width, height, 0, 0, width, height,
buf, bmpinfo, DIB_RGB_COLORS, SRCCOPY);
delete[] buf;
delete[] bmpinfo;
delete[] palette;
delete destination;
delete source;
}
void CMFCApplicationColorsView::OnDraw(CDC* pDC)
{
CMFCApplicationColorsDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
Gdiplus::GdiplusStartupInput tmp;
ULONG_PTR token;
Gdiplus::GdiplusStartup(&token, &tmp, NULL);
draw(pDC->GetSafeHdc());
Gdiplus::GdiplusShutdown(token);
}

How to display image using CreateDIBSection

I want to display a images on window, am giving image data as in buffer &CaptureBuffer to CreateDIBSection, CreateDIBForVideo method is calling from thread on main().
dont know where im going wrong it is showing black window.
void CreateDIBForVideo()
{
// ScreenCaptureProcessorGDI is a class it have initialization for capture window screen
screenObject = new ScreenCaptureProcessorGDI();
screenObject->init();
HDC DisplayDC = CreateDC((LPCWSTR)"DISPLAY", NULL, NULL, NULL);
BITMAPINFO bmpInfo = { 0 };
bmpInfo.bmiHeader.biSize= sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = screenObject->lOutputDuplDesc.ModeDesc.Width;
bmpInfo.bmiHeader.biHeight= screenObject->lOutputDuplDesc.ModeDesc.Height;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 32;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biSizeImage = (4 * screenObject->lOutputDuplDesc.ModeDesc.Width * screenObject->lOutputDuplDesc.ModeDesc.Height);
bmpInfo.bmiHeader.biXPelsPerMeter = 0;
bmpInfo.bmiHeader.biYPelsPerMeter = 0;
bmpInfo.bmiHeader.biClrUsed = 0;
bmpInfo.bmiHeader.biClrImportant = 0;
CaptureBuffer = NULL;
HDC pXorDC = CreateCompatibleDC(DisplayDC);
HBITMAP hXorDib = CreateDIBSection(DisplayDC, &bmpInfo, DIB_RGB_COLORS, (void**)&CaptureBuffer, NULL, 0);
hXorTemp = (HBITMAP)SelectObject(pXorDC, hXorDib);
// startGrab this thread capture a windows screen after init()
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&startGrab, NULL, 0, NULL);
}
void startGrab()
{
for (int index = 0; index < 100; index++)
{
// grabImage will capture window screen and send image as a buffer to `CaptureBuffer`
screenObject->grabImage();
PaintViewerWindow();
UpdateWindow(global_hWnd);
::Sleep(2000);
}
}
void PaintViewerWindow()
{
HDC paintDC;
PAINTSTRUCT ps;
paintDC = BeginPaint(global_hWnd, &ps);
SetStretchBltMode(paintDC, HALFTONE);
BitBlt(paintDC, 0, 0, 1366, 768, pXorDC, 0, 0, SRCCOPY);
EndPaint(global_hWnd, &ps);
}
I find a solution for the above problem
It because of UCHAR *CaptureBuffer am directly passing CaptureBuffer to the CreateDIBSection now i changed to copying UCHAR *CaptureBuffer to char *DisplayBuffer its working fine now.
UCHAR *CaptureBuffer = NULL;
char *DisplayBuffer = NULL;
long CaptureSize = NULL;
void saveImage(unsigned int frame_num, BITMAPINFO &lBmpInfo, std::unique_ptr<BYTE> pBuf, UCHAR* &CaptureBuffer, long &CaptureSize)
{
BITMAPFILEHEADER bmpFileHeader;
bmpFileHeader.bfReserved1 = 0;
bmpFileHeader.bfReserved2 = 0;
bmpFileHeader.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + lBmpInfo.bmiHeader.biSizeImage;
bmpFileHeader.bfType = 'MB';
bmpFileHeader.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
CaptureSize = lBmpInfo.bmiHeader.biSizeImage;
CaptureBuffer = (UCHAR*)malloc(CaptureSize);
memcpy(CaptureBuffer, pBuf.get(), lBmpInfo.bmiHeader.biSizeImage);
//Here am copying CaptureBuffer(uchar) to DisplayBuffer(char)
memcpy(DisplayBuffer, CaptureBuffer, CaptureSize);
lresult = 0;
}
void CreateDIBForVideo()
{
// ScreenCaptureProcessorGDI is a class it have initialization for capture window screen
screenObject = new ScreenCaptureProcessorGDI();
screenObject->init();
HDC DisplayDC = CreateDC((LPCWSTR)"DISPLAY", NULL, NULL, NULL);
BITMAPINFO bmpInfo = { 0 };
bmpInfo.bmiHeader.biSize= sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = screenObject->lOutputDuplDesc.ModeDesc.Width;
bmpInfo.bmiHeader.biHeight= screenObject->lOutputDuplDesc.ModeDesc.Height;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 32;
bmpInfo.bmiHeader.biCompression = BI_RGB;
bmpInfo.bmiHeader.biSizeImage = (4 * screenObject->lOutputDuplDesc.ModeDesc.Width * screenObject->lOutputDuplDesc.ModeDesc.Height);
bmpInfo.bmiHeader.biXPelsPerMeter = 0;
bmpInfo.bmiHeader.biYPelsPerMeter = 0;
bmpInfo.bmiHeader.biClrUsed = 0;
bmpInfo.bmiHeader.biClrImportant = 0;
CaptureBuffer = NULL;
HDC pXorDC = CreateCompatibleDC(DisplayDC);
HBITMAP hXorDib = CreateDIBSection(DisplayDC, &bmpInfo, DIB_RGB_COLORS, (void**)&DisplayBuffer, NULL, 0);
hXorTemp = (HBITMAP)SelectObject(pXorDC, hXorDib);
// startGrab this thread capture a windows screen after init()
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)&startGrab, NULL, 0, NULL);
}

why CreateDIBSection() fails with certian BITMAPINFO?

I am trying to use CreateDIBSection.
Problem:
In Windows XP, I tried to call CreateDIBSection, it returns NULL and GetLastError = 0
When I try to change the screen resolution, for example to 2048 x 1536, it returns correct value.
I have tested this function has some relationship with nMemSize (not necessarily small number).
Question:
Is there any guarantee way to ensure CreateDIBSection returns correct value?
nScreenWidth = 1024;
nScreenHeight= 768;
= nScreenWidth*nScreenHeight*3*7
HDC hdc = ::GetDC(hWnd);
m_hBmpMapFile = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, nMemSize, NULL);
BITMAPINFO bmpInfo = {0};
bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmpInfo.bmiHeader.biWidth = nScreenWidth;
bmpInfo.bmiHeader.biHeight = nScreenHeight;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 24;
bmpInfo.bmiHeader.biCompression = 0;
bmpInfo.bmiHeader.biSizeImage = nMemSize;
bmpInfo.bmiHeader.biXPelsPerMeter = 0;
bmpInfo.bmiHeader.biYPelsPerMeter = 0;
bmpInfo.bmiHeader.biClrUsed = 0;
bmpInfo.bmiHeader.biClrImportant = 0;
bmpInfo.bmiColors[0].rgbBlue = 204;
bmpInfo.bmiColors[0].rgbGreen = 204;
bmpInfo.bmiColors[0].rgbRed = 204;
bmpInfo.bmiColors[0].rgbReserved = 0;
PVOID pvBits;
m_hBmpAllWstDskWallpaper = ::CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, &pvBits, m_hBmpMapFile, 0);
I suspect the problem might be contained in the section of code that you did not include (in the elipses ...). So I recommend:
Check your Device Context is valid
ZeroMemory
Add the structure size
And bitmap dimensions
Move the GetLastError calls to ensure that the Device Context is valid (perhaps an earlier API call fails)
The code below seems to work after I added the recommendations above, I hope it helps:
HDC hdc = ::GetDC(hWnd);
int nScreenWidth = 1024;
int nScreenHeight= 768;
int nMemSize = nScreenWidth*nScreenHeight*3*7;
HANDLE m_hBmpMapFile = ::CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, nMemSize, NULL);
BITMAPINFO bmpInfo;
//clear the memory
ZeroMemory(&bmpInfo.bmiHeader, sizeof(BITMAPINFOHEADER));
//struct size
bmpInfo.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
//dimensions
bmpInfo.bmiHeader.biWidth = nScreenWidth;
bmpInfo.bmiHeader.biHeight = nScreenHeight;
bmpInfo.bmiHeader.biPlanes = 1;
bmpInfo.bmiHeader.biBitCount = 32;
bmpInfo.bmiHeader.biSizeImage=nMemSize;
void *pvBits = NULL;
HANDLE m_hBmpAllWstDskWallpaper = ::CreateDIBSection(hdc, &bmpInfo, DIB_RGB_COLORS, &pvBits, m_hBmpMapFile, 0);
int nError = ::GetLastError();