3

I am using Flatbuffers to serialize some data, which is then sent over the network via TCP. I can deserialize it just fine in the client application, but I have a problem with some image data that I am sending.

I have my flatbuffers schema defined as follows:

namespace datapacket;

table FlatDataPacket {
  id:string;
  framedata:[ubyte];
}

root_type FlatDataPacket;

I deserialize my data like so:

ByteBuffer bb = new ByteBuffer(buffer); 
var flatDataPacket = FlatDataPacket.GetRootAsFlatDataPacket(bb);

Now, in flatDataPacket, my Id is just fine, however, my imagedata length is 8 and when I try to convert to an image, I get an exception:

for (int i = 0; i < flatDataPacket.FramedataLength; i++)
{
    incomingPacket.FrameBytes[i] = flatDataPacket.GetFramedata(i);
}

Image returnImage = Image.FromStream(new MemoryStream(incomingPacket.FrameBytes));//Throws exception

Exception:

An exception of type 'System.ArgumentException' occurred in System.Drawing.dll but was not handled in user code

Additional information: Parameter is not valid.

What am I doing wrong? If I do this without flatbuffers and use either JSON or send just the image data, the image gets sent and deserialized just fine. Am I using the incorrect data type ubyte?

pookie
  • 3,796
  • 6
  • 49
  • 105
  • Usually errors like this occur because of the encoding. Sending images you need to use UTF8 encoding. Most stream classes default to use ASCII Encoding which will remove non-printable characters which will corrupt images. Start by verifying the original image byte count matches the received image byte count. Then find where the byte count has changed. – jdweng Jan 23 '16 at 14:08
  • You appear to be using the API correctly, so what likely happened is that something went wrong during transfer. Make sure whatever method you use for transfer is set to treat this data as **binary** (not UTF-8 as @jdweng suggests). – Aardappel Jan 23 '16 at 17:47
  • UTF8 is binary. Ignore Aardappel comments. – jdweng Jan 23 '16 at 20:22
  • Thank you, guys, but I have found my issue. It was that I had incorrectly set the size of the data being sent from my remote c++ application. Previously: `auto framedatavector = builder.CreateVector(data.GetData(), sizeof(data.GetData()));` New, working version: `auto framedatavector = builder.CreateVector(data.GetData(), data.Num());` – pookie Jan 23 '16 at 20:24

0 Answers0