I am using this function to take screenshot from a specific window or whole desktop, And it works because i am seeing the result with pasting into MSPaint. But i don't want to write the result to a *.bmp
or any type of file, I wanted to make a simple vnc client from scratch by sending this bitmap data to a remote server. Can i just send this bitmap directly through socket to the server? Or should i convert it to byte
or string
first.
static void MyPrintWindow(HWND hWnd)
{
RECT rc;
GetWindowRect(hWnd, &rc);
const HDC hScreenDC = GetDC(nullptr);
const HDC hMemoryDC = CreateCompatibleDC(hScreenDC);
const int width = GetDeviceCaps(hScreenDC, HORZRES);
const int height = GetDeviceCaps(hScreenDC, VERTRES);
hBitmap = CreateCompatibleBitmap(hScreenDC, width, height);
HBITMAP(SelectObject(hMemoryDC, hBitmap));
BitBlt(hMemoryDC, 0, 0, width, height, hScreenDC, 0, 0, SRCCOPY);
PrintWindow(hWnd, hMemoryDC, PW_CLIENTONLY);
OpenClipboard(nullptr);
EmptyClipboard();
SetClipboardData(CF_BITMAP, hBitmap);
CloseClipboard();
DeleteDC(hMemoryDC);
DeleteDC(hScreenDC);
}
This is the HBITMAP
to Byte
converting function that i tried which returns emtpy byte:
static byte BitmapToByte(HBITMAP bit)
{
BITMAP bitmap;
GetObject(bit, sizeof(BITMAP), &bitmap);
const int size = bitmap.bmHeight*bitmap.bmWidth*bitmap.bmBitsPixel / 8;
byte lp_bits;
GetBitmapBits(HBITMAP(bit), size, &lp_bits);
return lp_bits;
}
Also i have read some open source applications and they use this method but the converting part is too compilcated.
UPDATE: So i found this: c++ how to send Hbitmap over socket and it is close to what i want to do but the solution is incomplete, However i got the idea, Just get the pixels from the bitmap then send those pixels or saving those pixel data to array of bytes then send the data.