Saira
Saira

Reputation: 107

Size of byte array while converting writeableBitmap to byte array

I got this code to convert WriteableBitmap to byte array in my previous question Converting WriteableBitmap to Byte array - Windows phone 8.1 - Silverlight

public static byte[] ConvertToByteArray(WriteableBitmap writeableBitmap)
{
  using (var ms = new MemoryStream())
  {
    writeableBitmap.SaveJpeg(ms, 
         writeableBitmap.PixelWidth, writeableBitmap.PixelHeight, 0, 100);
    return ms.ToArray();
  }
}

This code works but it returns arrays of different lengths every time. Is it possible to get the array of same size each time? The writeableBitmaps that it gets as parameter are always of same size.

Upvotes: 0

Views: 218

Answers (1)

Alexei Levenkov
Alexei Levenkov

Reputation: 100545

Jpeg is compressed image format so unless your input has identical content all the time you'll get different length in byte (even for same size of an image).

Your options:

  • update code to deal with variable size of image bytes
  • use non-compressed format (will dramatically increase size of the data)
  • pad data with 0 at the end based on max expected size of the array (you'll probably have to block some images as otherwise the size will have to be comparable to size of uncompressed image)

Upvotes: 1

Related Questions