title: How to send bitmap image using tcp and socket class in c#
body: <p>I'm trying to make to add a simple desktop viewer to my application but I'm having problem sending the bitmap image to my server using socket class. I look over the net and all I find is to use stream and binaryformatter but it would change my whole network protocol. So I was hoping if someone here can help me.</p>
  
  <p>Here is my code for getting bitmap image and my send function.</p>
  
  <pre><code>private Image getDesktopImage()
  {
      Rectangle sb = Screen.PrimaryScreen.Bounds;
      Bitmap img = new Bitmap(sb.Width, sb.Height, PixelFormat.Format32bppArgb);
      using (Graphics g = Graphics.FromImage(img)) g.CopyFromScreen(sb.X, sb.Y, 0, 0, sb.Size, CopyPixelOperation.SourceCopy);
      return img;
  }
  
  public void sendData(string msg)
  {
      byte[] sdata = Encoding.ASCII.GetBytes(msg);
      clientSocket.Send(sdata, 0, sdata.Length, 0);
  }
  </code></pre>
  
  <p>and here is my function for receiving function</p>
  
  <pre><code>void clientHandle(object handle)
  {
      int _handle = (int)handle;
      try
      {
          byte[] receiveByte = new byte[1028];
          int buffer = serverSocket[_handle].Receive(receiveByte, 0, receiveByte.Length, 0);
          Array.Resize(ref receiveByte, buffer);
  
          if (Encoding.ASCII.GetString(receiveByte) == "")
          {
              DisconnectedEvent(_handle);
          }
          ReceivedEvent(_handle, Encoding.ASCII.GetString(receiveByte));
          clientHandle(_handle);
      }
      catch
      {
          DisconnectedEvent(_handle);
      }
  }
  </code></pre>
  
  <p>So my question is how can I convert the bitmap image so I can use my sendData function to send bitmap image. and then when i received it and convert it back to image. thanks</p>