title: TCP Client/Server Image Transfer
body: <p>I'm trying to send an image using a TCP socket. The client connects to the server without any problems and start to receive the data. The problem is when I try to convert the stream to an image using FromStream() method, I get an OutOfMemory Exception. Can anyone help me out? Really important!! Here is the code;</p>
  
  <h2>client snippet</h2>
  
  <p><br><pre><code>
  private void btnConnect_Click(object sender, EventArgs e)
          {
              IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
              TcpClient client = new TcpClient();</p>
  
  <pre><code>        client.Connect(ipAddress, 9500);
          NetworkStream nNetStream = client.GetStream();
  
          while (client.Connected)
          {
              lblStatus.Text = "Connected...";
              byte[] bytes = new byte[client.ReceiveBufferSize];
              int i;
              if (nNetStream.CanRead)
              {
                  nNetStream.Read(bytes, 0, bytes.Length);  
  
                  Image returnImage = Image.FromStream(nNetStream); //exception occurs here
                  pictureBox1.Image = returnImage;
              }
              else
              {
                  client.Close();
                  nNetStream.Close();
              }
  
  
          }
          client.Close();
      }
  </code></pre>
  
  <p></code></pre></p>
  
  <h2>server snippet</h2>
  
  <pre><code>
  try
              {
                  IPAddress ipAddress = Dns.Resolve("localhost").AddressList[0];
                  TcpListener server = new TcpListener(ipAddress, 9500);
                  server.Start();
                  Console.WriteLine("Waiting for client to connect...");
  
                  while (true)
                  {
                      if (server.Pending())
                      {
                          Bitmap tImage = new Bitmap(Image URL goes here);
                          byte[] bStream = ImageToByte(tImage);
  
                          while (true)
                          {
                              TcpClient client = server.AcceptTcpClient();
                              Console.WriteLine("Connected");
                              while (client.Connected)
                              {
                                  NetworkStream nStream = client.GetStream();
                                  nStream.Write(bStream, 0, bStream.Length);
                              }
                          }
                      }
                  }
  
              }
  
  
              catch (SocketException e1)
              {
                  Console.WriteLine("SocketException: " + e1);
              }
          }
          static byte[] ImageToByte(System.Drawing.Image iImage)
          {
              MemoryStream mMemoryStream = new MemoryStream();
              iImage.Save(mMemoryStream, System.Drawing.Imaging.ImageFormat.Gif);
              return mMemoryStream.ToArray();
          }
  
  </code></pre>
  
  <p>Thanks a lot in advanced,</p>