简体   繁体   English

C#-图像-二进制格式化程序反序列化错误-不包含有效的BinaryHeader

[英]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.

相关问题 二进制流'0'在反序列化时不包含有效的BinaryHeader错误 - Binary stream '0' does not contain a valid BinaryHeader error on deserialization 二进制流“ 226”不包含有效的BinaryHeader-C# - Binary stream '226' does not contain a valid BinaryHeader - C# 反序列化自定义对象时,二进制流“0”不包含有效的BinaryHeader错误 - Binary stream '0' does not contain a valid BinaryHeader error while Deserialization custom object 二进制流“ NN”不包含有效的BinaryHeader - Binary stream 'NN' does not contain a valid BinaryHeader 二进制流“ 0”不包含有效的BinaryHeader - Binary stream '0' does not contain a valid BinaryHeader 二进制流“ 0”不包含有效的BinaryHeader - Binary stream '0' does not contain a valid BinaryHeader 二进制流'0'不包含有效的BinaryHeader。 随机发生 - Binary stream '0' does not contain a valid BinaryHeader. Occurs randomly 令人沮丧的TCP序列化异常:二进制流“ 0”不包含有效的BinaryHeader - Frustrating TCP Serialization Exception: Binary stream '0' does not contain a valid BinaryHeader C#二进制格式化程序序列化和反序列化-3个随机异常 - C# Binary Formatter Serialization & Deserialization - 3 Random Exceptions C#二进制反序列化错误,类修改 - C# Binary Deserialization Error, class modification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM