通过winsock发送位图数据? Winapi

通过winsock发送位图数据? Winapi

问题描述:

我打算通过winsock发送桌面截图。通过winsock发送位图数据? Winapi

因此,有四项任务:

Save bitmap to buffer 
Write data across wire using a socket 
Read data from wire using a socket 
Load a bitmap from a buffer 

我已保存的位图使用的GetDIBits一个字符数组。 将数据写入服务器,我已经完成,但我有问题。 要将数据从服务器写入客户端,是否需要仅使用1个recv()调用(我正在使用TCP),还是需要将它分解为多个部分?我读过TCP是流的概念,我不会担心数据包,因为它是为我抽象的?

我该如何将GetDIBits中的信息加载到位图中并将其显示在主窗口上?

我猜我不得不使用SetDIBits,但我使用哪种设备上下文?

服务器截图的捕捉是在这里:

HDC handle_ScreenDC = GetDC(NULL); 
HDC handle_MemoryDC = CreateCompatibleDC(handle_ScreenDC); 
BITMAP bitmap; 


int x = GetDeviceCaps(handle_ScreenDC, HORZRES); 
int y = GetDeviceCaps(handle_ScreenDC, VERTRES); 

HBITMAP handle_Bitmap = CreateCompatibleBitmap(handle_ScreenDC, x, y); 
SelectObject(handle_MemoryDC, handle_Bitmap); 

BitBlt(handle_MemoryDC, 0, 0, x, y, handle_ScreenDC, 0, 0, SRCCOPY); 

GetObject(handle_Bitmap, sizeof(BITMAP), &bitmap); 

BITMAPFILEHEADER bmfHeader; 
BITMAPINFOHEADER bi; 

bi.biSize = sizeof(BITMAPINFOHEADER); 
bi.biWidth = bitmap.bmWidth; 
bi.biHeight = bitmap.bmHeight; 
bi.biPlanes = 1; 
bi.biBitCount = 16; 
bi.biCompression = BI_RGB; 
bi.biSizeImage = 0; 
bi.biXPelsPerMeter = 0; 
bi.biYPelsPerMeter = 0; 
bi.biClrUsed = 0; 
bi.biClrImportant = 0; 
//std::cout<< bitmap.bmWidth; 
DWORD dwBmpSize =((bitmap.bmWidth * bi.biBitCount + 5)/32) * 4 * bitmap.bmHeight; 
//int i = bitmap.bmWidth; 
//DWORD dwBmpSize = 99; 

HANDLE hDIB = GlobalAlloc(GHND, dwBmpSize); 

char* bufptr = (char *)GlobalLock(hDIB); 

GetDIBits(handle_ScreenDC, handle_Bitmap, 0, (UINT)bitmap.bmHeight, bufptr, (BITMAPINFO *)&bi, DIB_RGB_COLORS); 


send(clientsock, bufptr , GlobalSize((char *)GlobalLock(hDIB)), 0); 
/*Do i need to packetize/split it up? Or 1 send() is good for the matching Recv on the client?*/ 
/*I am assuming i must send bi structure over winsock also correct?*/ 

和receiveing客户端代码:

 case WM_PAINT:{ 

     //Im a Gdi beginner so I dont have a clue what im doing here as far as blitting the recved bits, this is just some stuff i tried myself before asking for help 

     PAINTSTRUCT paintstruct; 

     HDC handle_WindowDC = BeginPaint(hwnd, &paintstruct); 

     handle_MemoryDC = CreateCompatibleDC(handle_WindowDC); 


     handle_Bitmap = CreateCompatibleBitmap(handle_WindowDC, 640, 360); 


     std::cout << SetDIBits(handle_MemoryDC, handle_Bitmap, 0, bi.biHeight, buffer, (BITMAPINFO *)&bi, DIB_RGB_COLORS); 

     SelectObject(handle_MemoryDC, handle_Bitmap); 

     StretchBlt(handle_WindowDC, 50, 50, 640, 360, handle_MemoryDC, 0, 0, x, y, SRCCOPY); 


     EndPaint(hwnd, &paintstruct); 

    } 

套接字确实有两端有限的缓冲区大小,通常约为4000字节。因此,如果在一次调用非阻塞发送的过程中转储大块数据(如完整的screendump),您可能会遇到错误,您将需要管理自己的缓冲区,调用多次发送。但是,如果您使用的是非阻塞套接字,则应该正常,因为send()将会阻塞,直到发送完所有数据为止。

在接收端,它是一个类似的情况 - 阻塞接收可以持续等待,直到它具有您要求的完整数据大小,但是非阻塞接收将返回当前可用的任何数据,这将导致数据逐位过滤,您将需要重新组合来自多个recv()调用的数据。

我听说一次发送真正大块数据的问题,所以如果你一次发送5兆字节的数据,请注意可能还会有其他问题发挥作用。