[win32] Send bitmap over socket

I want to send a bitmap over socket, however since I kinda work with bitmaps for the first time, I dont really have an idea how to do that, so far I have code that gets the screenshot of the screen and stores it to HBITMAP (works fine), then fills BITMAPINFO struct and then creates a BYTE buffer from that struct, so I can send it via socket and then somehow (i still dont know how) recreate the bitmap using the BYTE buffer I created

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
				RECT rc;
				GetClientRect(GetDesktopWindow(), &rc);

				HDC hDC = GetDC(0);
				HBITMAP hBmp = CreateCompatibleBitmap(hDC, rc.right, rc.bottom);

				SelectObject(hDC, hBmp);

				//Draws screenshot to the hBmp
				BitBlt(hDC, 0, 0, rc.right, rc.bottom, GetDC(NULL), 0, 0, SRCCOPY);

				BYTE* buf = new BYTE[256*256*256];
				BITMAPINFO bmi;

				int a = GetDIBits(hDC, hBmp, 0, rc.bottom, NULL, &bmi, DIB_RGB_COLORS);
				if(a == 0)
					MessageBox(hWnd, "Error when filling BMI", NULL, NULL);

				int b = GetDIBits(hDC, hBmp, 0, rc.bottom, buf, &bmi, DIB_RGB_COLORS);
				if(b == 0)
					MessageBox(hWnd, "Error when filling buffer", NULL, NULL);
				
				//hBmp == screenshot

				ReleaseDC(hWnd, hDC);
				DeleteDC(hDC);
				DeleteObject(hBmp);


so the code is supposed to:

1. get dimensions of screen
2. create DC and compatible bitmap to store the screenshot
3. draw screen to bitmap
4. fill BITMAPINFO struct bmi with hBmp properties
5. fill BYTE buffer buff with BITMAPINFO properties we got from point 4
6. at this point, I should be able to send buffer, that contains everything needed to recreate bitmap (or?)
7. free memory

HOWEVER I'm getting "Error when filling BMI" error, and I have no idea why, also, how do I recreate bitmap from the "buf" on another client after I get it from GetDIBits(hDC, hBmp, 0, rc.bottom, buf, &bmi, DIB_RGB_COLORS);
Last edited on
Topic archived. No new replies allowed.