-2

I am trying to build a c# video communication app.Because udp has a size limit I'm trying to lower the image size .I tried this code here Image bitmap = pictureBox1.Image; ImageCodecInfo myImageCodecInfo; Encoder myEncoder; EncoderParameter menter code here`yEncoderParameter; EncoderParameters myEncoderParameters;

            myImageCodecInfo = GetEncoderInfo("image/jpeg");
            myEncoderParameters = new EncoderParameters(1);
            myEncoder = Encoder.Quality;
            myEncoderParameter = new EncoderParameter(myEncoder, 5L);
            myEncoderParameters.Param[0] = myEncoderParameter;

            bitmap.Save("Img.jpg", myImageCodecInfo, myEncoderParameters);
            Image snd = Image.FromFile("Img.jpg");
            string Ip = "127.0.0.1";
            int port = 1111;

            byte[] sndcam = ImageToByteArray(snd);

            IPEndPoint ep = new IPEndPoint(IPAddress.Parse(Ip), port);
            Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Dgram,ProtocolType.Udp);
            client.SendTo(sndcam, ep);`

but I still get the high file size error.I am open to all advice Thanks in advance

1 Answers1

1

Right, you get a SocketException (message too long) if you try to send more bytes at once than the underying MTU size (smalles MTU of entire path), thats by concept.

Solution is sending chunks of the data, Socket doesn't do that for you.

Something like that should do:

//client.SendTo(sndcam, ep);
client.Connect(ep);
client.SendBufferSize = 1024;
for (int pos = 0; pos < sndcam.Length; pos += client.SendBufferSize)
{
    int len = sndcam.Length - pos > client.SendBufferSize ? client.SendBufferSize : sndcam.Length - pos;
    byte[] chunk = new byte[len];
    Array.Copy(sndcam, pos, chunk, 0, chunk.Length);
    client.Send(chunk);
}
client.Close();

Remember that udp doesn't provide handshaking, meaning you never know if packets arrive in correct order or even if at all.

DerSchnitz
  • 457
  • 3
  • 8