title: Sending Bitmap data over winsock? Winapi
body: <p>I am tring to send a screenshot of a desktop over winsock.</p>
  
  <p>As such, there are four tasks:</p>
  
  <pre><code>Save bitmap to buffer
  Write data across wire using a socket
  Read data from wire using a socket
  Load a bitmap from a buffer
  </code></pre>
  
  <p>I have saved the bitmap to a char array using GetDIBits.
  Writing the data to the server, I have done but I have questions.
  For writing data over from server to the client, do I need to use only 1 recv() call (I am using TCP), or do i need to split it up into multiple parts? Ive read that TCP is stream concept and that I wouldnt have to worry about packets because that is abstracted for me?</p>
  
  <p>How would I go about loading the information from GetDIBits into a bitmap and displaying it on the main window?</p>
  
  <p>I am guessing I have to use SetDIBits, but into which device contexts do i use? </p>
  
  <p>The Server screenshot capturer is here:</p>
  
  <pre><code> 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), &amp;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&lt;&lt; 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 *)&amp;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?*/
  </code></pre>
  
  <p>And The receiveing client code:</p>
  
  <pre><code>        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, &amp;paintstruct);
  
          handle_MemoryDC = CreateCompatibleDC(handle_WindowDC);
  
  
          handle_Bitmap = CreateCompatibleBitmap(handle_WindowDC, 640, 360);
  
  
           std::cout &lt;&lt; SetDIBits(handle_MemoryDC, handle_Bitmap, 0, bi.biHeight, buffer, (BITMAPINFO *)&amp;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, &amp;paintstruct);
  
      }
  </code></pre>