Trying to return a byte stream using GetDIBits()

I am trying to simply capture the display of a single monitor. The following program creates a new DC and Bitmap from the last handle passed by EnumDisplayMonitors callback. It compiles with no errors but GetDIBits returns 0. I cannot find the source of the problem, what can I do to get the valid information from GetDIBits?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <windows.h>
#include <iostream>

BOOL CALLBACK MyInfoEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
	*reinterpret_cast<HMONITOR*>(dwData) = hMonitor;
	return true;
}

int main()
{
	HMONITOR monitorHandle = NULL;
	EnumDisplayMonitors(NULL, NULL, MyInfoEnumProc, reinterpret_cast<LPARAM>(&monitorHandle));

	MONITORINFOEX iMonitor;
	iMonitor.cbSize = sizeof(MONITORINFOEX);
	GetMonitorInfo(monitorHandle, &iMonitor);

	HDC hDisplay = CreateDC(iMonitor.szDevice, iMonitor.szDevice, NULL, NULL);

	int lenX = (iMonitor.rcWork.right - iMonitor.rcWork.left);
	int lenY = (iMonitor.rcWork.bottom - iMonitor.rcWork.top);

	HBITMAP hBitmap = CreateCompatibleBitmap(hDisplay, lenX, lenY);
	BITMAPINFO iBitmap = { 0 };

	iBitmap.bmiHeader.biSize = sizeof(iBitmap.bmiHeader);
	iBitmap.bmiHeader.biWidth = lenX;
	iBitmap.bmiHeader.biHeight = lenY;
	iBitmap.bmiHeader.biPlanes = 1;
	iBitmap.bmiHeader.biBitCount = 32;
	iBitmap.bmiHeader.biCompression = BI_RGB;
	iBitmap.bmiHeader.biSizeImage = lenX * 3 * lenY;
	iBitmap.bmiHeader.biClrUsed = 0;
	iBitmap.bmiHeader.biClrImportant = 0;

	BYTE* bStream = new BYTE[iBitmap.bmiHeader.biSizeImage];

	if (0 == (GetDIBits(hDisplay, hBitmap, 0, iBitmap.bmiHeader.biHeight, bStream, &iBitmap, DIB_RGB_COLORS)))
	{
		//Error handling
		std::cout << "Error 1" << std::endl;
	}

	system("pause");
	return 0;
}
Topic archived. No new replies allowed.