title: C# Sending image on tcp client/server
body: <pre><code>  im trying to make a client and server that the server sends image to the client
  </code></pre>
  
  <p>so far it possible to send only one image and the others arent accepted by mysterious error
  i tried to figure out the errors but no luck...
  this is the Server code:
    public void SendImage(Image img)
          {</p>
  
  <pre><code>        TcpClient tempClient = _Client;
  
          if (tempClient.Connected) //If the client is connected
          {
              NetworkStream stream = tempClient.GetStream();
              Bitmap bmp = new Bitmap(img);
              BinaryWriter bw = new BinaryWriter(stream);
              MemoryStream ms = new MemoryStream();
              bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
              byte[] buffer = ms.ToArray();
            byte[] lengthbts=  Get8LengthByte(buffer);
            stream.Write(lengthbts,0,8);
            stream.Write(buffer, 0, buffer.Length );
            stream.Flush();
          //   bw.Close();
  
  
  
          }
  
      }
      byte[] Get8LengthByte(byte[] bytes)
      {
          string length = bytes.Length.ToString();
          while (length.Length &lt; 8)
          {
              length = "0" + length;
          }
          return Encoding.Default.GetBytes(length);
      }
  </code></pre>
  
  <p>and this is the client code</p>
  
  <pre><code>       NetworkStream stream = client.GetStream();
              //Bitmap bmp = new Bitmap(img);
              BinaryReader br = new BinaryReader(stream);
              byte[] datalen = new byte[8];
              br.Read(datalen, 0, 8);
              string lenStr = (Encoding.Default.GetString(datalen));
              int len = int.Parse(lenStr);//BitConverter.ToInt32(datalen, 0);//
              Console.WriteLine(len);
              byte[] buffer = new byte[len];
              br.Read(buffer, 0, buffer.Length);
                MemoryStream ms = new MemoryStream(buffer, 0, buffer.Length);
              ms.Position = 0;
              Image img = Image.FromStream(ms);
              Invoke(new MethodInvoker(delegate()
              {
                  pictureBox1.Image = img;
              }
              ));
  </code></pre>
  
  <p>the idea is to send the length of the pictures bytes first as 8 byte length
  for example if the byte length is 10 then the 8 byte length is "00000010"
  the first image recieved is very good and quick
  but the second return an error that can make the 8 byte length to integer the byte[] recieved are more like "?///???" and stuff like that if someone could help me to figure it out i will be very thankful</p>