ToByteArray returns all zeros

Aug 16, 2014 at 5:36 PM
I have the following code for a WinRT project:
Async Function SetImage(imagestream As IRandomAccessStream, image As Image) As Task
    Dim Bitmap As New BitmapImage
    Dim Bytes As Byte()
    ' Note
    Bitmap.SetSource(imagestream) ' I get the width and height of the image from this
    Dim Wbm as WriteableBitmap = New WriteableBitmap(Bitmap.PixelWidth, Bitmap.PixelHeight)
    image.Source = Await Wbm.FromStream(imagestream)
    Bytes = Wbm.ToByteArray

End Function
The image appears correctly on-screen after this function
However, The Bytes array contains all zeros. I need the byte array to store the image in my program's database. It obviously isn't working.

Is there something I'm not understanding about 'ToByteArray'?
Aug 16, 2014 at 8:45 PM
Update: I've determined that the problem lies with this line of code:
image.Source = Await Wbm.FromStream(imagestream)
Reading from the stream does not work. I've tried closing and reopening imagestream before reading, but that does not work either.

I tried converting the imagestream directly into a byte array and that at least produced 'something'. Which I could successfully save and the load from my database and the exact same 'thing' would reappear upon loading. Unfortunately, the image does not resemble in any way the actual image being loaded.

If someone can point me to a way to convert the imagestream into a proper byte array for WriteableBitmapEx I'd be home free. Otherwise if someone can help me figure out why reading from the stream isn't working that would do the trick too.

Thanks in advance for any help!