[英]C# - Image - Binary Formatter Deserialization Error - Does not contain a valid BinaryHeader
I've made a program which is sending images over TCP sockets, it can be running for a while then always on the server side it has an error deserializing the stream. 我制作了一个通过TCP套接字发送图像的程序,它可以运行一段时间,然后总是在服务器端反序列化流时出错。
Additional information: Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.
The full function which is causing the error is: 引起错误的完整功能是:
private void handleClientThread(TcpClient tcpClient)
{
while (true)
{
BinaryFormatter binaryFormatter = new BinaryFormatter();
pictureBox1.Image = (Image)binaryFormatter.Deserialize(tcpClient.GetStream()); // this line is the one throwing an exception
pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
}
}
The client is sending everything correctly: 客户端正确发送了所有内容:
private Image TakePicture()
{
Bitmap bmpScreenCapture = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
Graphics g = Graphics.FromImage(bmpScreenCapture);
g.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, bmpScreenCapture.Size, CopyPixelOperation.SourceCopy);
return bmpScreenCapture;
}
private void SendPicture()
{
Image clientPicture = TakePicture();
BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(sharerClient.GetStream(), clientPicture);
}
Could anyone help me with this issue or help me send the images over TCP sockets without it throwing an exceptions. 任何人都可以帮助我解决此问题,或者帮助我通过TCP套接字发送图像而不会引发异常。
private void SendPicture()
{
Image clientPicture = TakePicture();
clientPicture.Save(sharerClient.GetStream(), ImageFormat.Png);
}
I came up with that Lucas Trzesniewski, but how would I do it on the server side? 我想到了Lucas Trzesniewski,但是我将如何在服务器端做到这一点?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.