title: Trouble with image transferring over TCP sockets in C#
body: <p>What I am trying to do is to transfer an image of the client's screen to the server.
  To do so, in the client side, I get the screen's image, convert it to byte array and send the byte array to the server over TCP sockets.
  In the server side, I convert the byte array from the client to image and put it in pictureBox.</p>
  
  <p>Well, the result seems to be cool on the client side, but the picture I get on the server side is distorted:
  <img src="https://i.stack.imgur.com/a5Vxi.png" alt="http://i.stack.imgur.com/wsLNh.png"></p>
  
  <p>The right picture is the client side (saves the screen and sends it to the server),
  and the server side is on the left.
  As you can see on the server's side, the picutre is not full.</p>
  
  <p>I am using these codes to send an image, recieve the image and to convert (byte > image || image > byte):</p>
  
  <pre><code>private Image getScreen()
  {
      Size s = Screen.PrimaryScreen.Bounds.Size;
      Bitmap b = new Bitmap(s.Height, s.Width);
      Graphics g = Graphics.FromImage(b);
      g.CopyFromScreen(0, 0, 0, 0, s);
  
      return b;
  }
  
  private byte[] BmpToBytes(Image bmp)
  {
      MemoryStream ms = new MemoryStream();
      bmp.Save(ms, ImageFormat.Jpeg);
      byte[] bmpBytes = ms.GetBuffer();
      bmp.Dispose();
      ms.Close();
  
      return bmpBytes;
  }
  
  private Image BytesToImg(byte[] bmpBytes)
  {
      MemoryStream ms = new MemoryStream(bmpBytes);
      Image img = Image.FromStream(ms);
      return img;
  }
  
  
  Client side:
  private void SendScreenToServer()
  {
      while (true)
      {
          byte[] sendBytes = new byte[12500];
          sendBytes = BmpToBytes(getScreen());
          pictureBox1.Image = getScreen();
          serverStream.Write(sendBytes, 0, sendBytes.Length);
          serverStream.Flush();
      }
  }
  
  Server side:
  private void GetScreenFromClient()
  {
      while (true)
      {
          byte[] bytesFrom = new byte[12500];
          networkStream.Read(bytesFrom, 0, clientSocket.ReceiveBufferSize);
  
          try
          {
              pictureBox1.Image = BytesToImg(bytesFrom);
          }
          catch { }
      }
  }
  </code></pre>
  
  <p>Anyone has idea what am I doing wrong? or is there any other option to transfer image over sockets?</p>