title: [MFC/C++]Sending CBitmap's bits over socket, and re-construct it on receiver side
body: <p>I am new with MFC and try to learn it with a project of MFC dialog base on VS2008. Here are the archivements I have done:</p>
  
  <p>First, I have managed to display a list of pictures from a folder to a Listbox Control. After that, I also handled the click event on each line of the listbox to load and show the picture to the Picture Control(type Bitmap) on the right side. You can see the image below for easy understanding: <a href="http://i.stack.imgur.com/ORySi.png" rel="nofollow">Please click here for the image of my MFC dialog</a>
  <br>
  <br>Here is the code. Note <code>m_ListCtrl</code> and <code>static_picture</code> are variables of the listbox and the picture control:</p>
  
  <pre><code>void CMyClientDlg::OnLbnSelchangeList1(){
  
  CString imagePath;
  m_ListCtrl.GetText(m_ListCtrl.GetCurSel(),imagePath);
  
  CImage picture;
  picture.Load(imagePath);
  
  if (!picture.IsNull())
  {
      float screenWidth = 200, screenHeight = 200;
      float imageWidth = picture.GetWidth();
      float imageHeight = picture.GetHeight();
  
      //scaling:
      float pictureRatio = imageWidth/ imageHeight;
      float newImageWidth;
      float newImageHeight;
      int aligmentX = 0;
      int aligmentY = 0;
      if (pictureRatio &lt;= 1)
      {
          newImageWidth = imageWidth*(screenHeight/imageHeight);
          newImageHeight = screenHeight;
          aligmentX = (screenWidth-newImageWidth)/2;
      }
      else
      {
          newImageWidth = screenWidth;
          newImageHeight = imageHeight*(screenWidth/imageWidth);
          aligmentY = (screenHeight - newImageHeight)/2;
      }
      //end scaling.
      CDC *screenDC = GetDC();
      CDC mDC;
      mDC.CreateCompatibleDC(screenDC);
  
      CBitmap bitMap;
      bitMap.CreateCompatibleBitmap(screenDC, screenWidth, screenHeight);
      CBitmap *pob = mDC.SelectObject(&amp;bitMap);
      mDC.SetStretchBltMode(HALFTONE);
      picture.StretchBlt(mDC.m_hDC, aligmentX, aligmentY, newImageWidth, newImageHeight, 0, 0, imageWidth, imageHeight, SRCCOPY);
      mDC.SelectObject(pob);
  
      /*.......code to convert bitmap to BYTE* ........*/
      /*.......code to send BYTE* over socket........*/       
  
      //display the bit map
      static_picture.SetBitmap((HBITMAP)bitMap.Detach());
  
      //clean up
      ReleaseDC(screenDC);
  }   
  </code></pre>
  
  <p>}<br>
  <br>So now I would like to advance one more step, and tried to work with socket... and yes, I successfully sent and received simple char* or CString over socket.
  <br>What I want to do is: instead showing the picture on this dialog, it shows the image on the other dialog(server).
  <br>Somehow I learned that there are 2 funtions that sound work: <code>SetBitmapBits()</code> and <code>GetBitmapBits()</code> (I honestly just read it on some source and have no idead if they suitable for my goal here).
  <br><br>So, I added this piece of code to turn the above bitmap into array of BYTE <code>bmpBuffer</code>:</p>
  
  <pre><code>BITMAP bmpProperties;
  bitMap.GetBitmap(&amp;bmpProperties);
  int bmpDemension = bmpProperties.bmWidthBytes*bmpProperties.bmHeight;
  BYTE* bmpBuffer=(BYTE*)GlobalAlloc(GPTR, bmpDemension);
  bitMap.GetBitmapBits(bmpDemension,bmpBuffer);
  </code></pre>
  
  <p>Then send that array over socket:</p>
  
  <pre><code>UpdateData(TRUE);
  char *socketBuffer = reinterpret_cast&lt;char*&gt;(bmpBuffer);
  send(m_ClientSocket, socketBuffer, sizeof(socketBuffer), 0);
  //clean up after send
  GlobalFree((HGLOBAL)bmpBuffer);
  </code></pre>
  
  <p>On the other dialog. Note: I have hardcoded the demension of the bitmap to 160000, just to simplify the problem:</p>
  
  <pre><code>void CMyServer2Dlg::OnReceive(){    
  char *socketBuffer = new char [1025];
  int iLen; 
  iLen = recv(m_sConnected, socketBuffer, 1025, NULL);
  if(iLen==SOCKET_ERROR)
  {
      AfxMessageBox("Could not Receive");
  }
  else
  {
      BYTE* bmpBuffer = reinterpret_cast&lt;BYTE*&gt;(socketBuffer);
  
      //re-construct the bitmap
      CBitmap clone;
      CDC *screenDC = GetDC();
      CDC mDC;
      mDC.CreateCompatibleDC(screenDC);
  
      clone.CreateCompatibleBitmap(screenDC, 200, 200);
      clone.SetBitmapBits(160000,bmpBuffer);
  
      //Picture control(type bitmap) has variable "static_picture"
      static_picture.SetBitmap((HBITMAP)clone.Detach());
  
      UpdateData(FALSE);
      ReleaseDC(screenDC);
      GlobalFree((HGLOBAL)bmpBuffer);
  }
  delete socketBuffer;
  </code></pre>
  
  <p>And, it just doesn't work... Please tell me where did I mess it up? And sorry for the long post.....</p>