BinaryFormatter.Deserialization SerializationException
-
I have been working on this issue for a few days now and I am growing tired and desperate :sigh: I have made this application which sends a screenshot (Image type) to another PC. All works well on most PCs except some very few of them. Those that do not work complain of a Binary stream '0' does not contain a valid BinaryHeader. The code is exactly the same on the working / non-working PCs. I have also attempted to run the program locally on the PCs not working, and the exact problem occurs. The Algorithm is as follows: - Screenshot is taken (returned as Image) - Size of the Image is sent (to prepare a byte[] for the receiving end) - Image is converted into a MemoryStream from BinaryFormatter.Serialize - MemoryStream.ToArray() gets byte[] which is written on a NetworkStream - Receiving end receives the length of the object and prepares a byte[] with that length - Receiving end reads the data on a byte[] - Byte[] is Deserialized to object <- Exception occurs X| I have checked the first 50 bytes being sent and received and they are exactly identical!! :doh: It is true that the first byte is a 0 but this works with no problems at all on other systems Why is this happening? How can I avoid this? Maybe using UnsafeDeserialize or something completely different?! :confused: Any help is sooo greatly appreciated!!
-
I have been working on this issue for a few days now and I am growing tired and desperate :sigh: I have made this application which sends a screenshot (Image type) to another PC. All works well on most PCs except some very few of them. Those that do not work complain of a Binary stream '0' does not contain a valid BinaryHeader. The code is exactly the same on the working / non-working PCs. I have also attempted to run the program locally on the PCs not working, and the exact problem occurs. The Algorithm is as follows: - Screenshot is taken (returned as Image) - Size of the Image is sent (to prepare a byte[] for the receiving end) - Image is converted into a MemoryStream from BinaryFormatter.Serialize - MemoryStream.ToArray() gets byte[] which is written on a NetworkStream - Receiving end receives the length of the object and prepares a byte[] with that length - Receiving end reads the data on a byte[] - Byte[] is Deserialized to object <- Exception occurs X| I have checked the first 50 bytes being sent and received and they are exactly identical!! :doh: It is true that the first byte is a 0 but this works with no problems at all on other systems Why is this happening? How can I avoid this? Maybe using UnsafeDeserialize or something completely different?! :confused: Any help is sooo greatly appreciated!!
Hi, I don't know what problem you are having. One possibility is the (de)serializer is having an out-of-memory problem due to fragmentation in the large-object-heap. Why do you need serialization at all? Couldn't you use Bitmap.Save(NetworkStream) and new Bitmap(NetworkStream)? I don't like your scheme requiring a potentially large array that much. :)
Luc Pattyn [Forum Guidelines] [My Articles]
The quality and detail of your question reflects on the effectiveness of the help you are likely to get. Show formatted code inside PRE tags, and give clear symptoms when describing a problem.