title: C# Flatbuffers byte array
body: <p>I am using <a href="https://google.github.io/flatbuffers/index.html" rel="nofollow">Flatbuffers</a> 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.</p>
  
  <p>I have my flatbuffers schema defined as follows:</p>
  
  <pre><code>namespace datapacket;
  
  table FlatDataPacket {
    id:string;
    framedata:[ubyte];
  }
  
  root_type FlatDataPacket;
  </code></pre>
  
  <p>I deserialize my data like so:</p>
  
  <pre><code>ByteBuffer bb = new ByteBuffer(buffer); 
  var flatDataPacket = FlatDataPacket.GetRootAsFlatDataPacket(bb);
  </code></pre>
  
  <p>Now, in <code>flatDataPacket</code>, my <code>Id</code> is just fine, however, my <code>imagedata</code> length is 8 and when I try to convert to an image, I get an exception:</p>
  
  <pre><code>for (int i = 0; i &lt; flatDataPacket.FramedataLength; i++)
  {
      incomingPacket.FrameBytes[i] = flatDataPacket.GetFramedata(i);
  }
  
  Image returnImage = Image.FromStream(new MemoryStream(incomingPacket.FrameBytes));//Throws exception
  </code></pre>
  
  <p><strong>Exception</strong>:</p>
  
  <blockquote>
    <p>An exception of type 'System.ArgumentException' occurred in
    System.Drawing.dll but was not handled in user code</p>
    
    <p>Additional information: Parameter is not valid.</p>
  </blockquote>
  
  <p>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 <code>ubyte</code>?</p>