Byte Array (write size and Read)

  • Thread starter Thread starter BKoshy
  • Start date Start date
B

BKoshy

Guest
I am writing some content to a binary file. one item is an image, so converting image in to a byte array and writing it as below.


BinaryWriter bw = (BinaryWriter)writer;

WritePropertyTag(property); //IMAGE_TAG

bw.Write((short)(sizeofshort + imagedata.Length + (writesize ? sizeofshort : 0)));

if (writesize)

{ bw.Write((short)(imagedata.Length)); }

bw.Write(imagedata);


and reads it back with the below code:


short datasize = binReaderIn.ReadInt16();

byte[] data = new byte[datasize];

binReaderIn.Read(data, 0, datasize);

Image img = (Bitmap)((new ImageConverter()).ConvertFrom(data));


The above code works very well if the byte array is relatively smaller (Short numbers).

When we want to store larger images (with approx byte array size 25k) it fails. I tried using long and int instead of short in the above code blocks and read with ReadInt32() or ReadInt64() method, but not getting the right byte array size as stored.

No error observed while writing but while reading the array size back, getting a wrong value.

Please help. Thanks.

Continue reading...
 
Back
Top