[win32api] Save BITMAP to file

I have this code to capture whole screen as bitmap, how do I save it to file? I've been trying to find solution for over a hour, can't find anything working

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
void TakeSnapshot(string directory)
{
	RECT rc;
	HWND hWnd2 = GetDesktopWindow();
	GetClientRect(hWnd2, &rc);
	HDC hScreenDC = CreateDC("DISPLAY", NULL, NULL, NULL);
	HDC hMemoryDC = CreateCompatibleDC(hScreenDC);

	HBITMAP hBitmap = CreateCompatibleBitmap(hScreenDC, rc.right, rc.bottom);
	
	SelectObject(hMemoryDC, hBitmap);

	BitBlt(hMemoryDC, 0, 0, rc.right, rc.bottom, GetDC(0), 0, 0, SRCCOPY);
	string a = "\\";
	stringstream ss;
	char b[8];
	ss << NOScreen;
	ss >> b;
	a += b;
	a += ".bmp";
	directory += a;
	NOScreen++;

	DeleteDC(hMemoryDC);
	DeleteDC(hScreenDC);
}
Last edited on
someone?
Look at this:

http://www.codeproject.com/Articles/9140/Saving-a-Drawing-to-a-Bitmap-File

Especially void CDrawing2BitmapDlg::OnSave()

it shows how to do that
im using plain win32 api (look at the title), not mfc
mfc is not far from plain api.

In the OnSave() the mfc stuff is not relevant. You can remove it.
The remaining code shows how to save a BITMAP struct to a file. Just take a closer look.
Topic archived. No new replies allowed.