I have a small problem, I am doing a video transmission between 2 PCs by TCP connection, what I do is take a screenshot of the screen, serialize it and send it to the other PC, I did tests from a single PC and everything goes very well but when I connect to the second pc the image is distorted if I use MemoryStream but the image update is fast but if I use BinaryFormatter the image arrives correctly but it takes time to update the image, here is an example:
This image is with MemoryStream
This is the new code that throws me the ArgumentException exception:
public static void SerializeScreen(Stream stream, Bitmap Image)
{
BinaryWriter binaryWriter = new BinaryWriter(stream);
MemoryStream memory = new MemoryStream();
Image.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
binaryWriter.Write(memory.ToArray());
}
public static Image DeserializeScreen(Stream stream)
{
byte[] Buffer = new byte[500000];
BinaryReader binaryReader = new BinaryReader(stream);
int Bytes;
int LastPost = 0;
do
{
Bytes = binaryReader.Read(Buffer, LastPost, Buffer.Length - LastPost);
LastPost += Bytes;
} while (Bytes > 0);
MemoryStream memory = new MemoryStream(Buffer);
return Image.FromStream(memory);
}
This line of code "stream.Read(Buffer, 0, Buffer.Length);" does not guarantee that the entire stream is read, you must loop until the entire image is read, something like: