Jon Maximys
Jon Maximys

Reputation: 75

empty byte array c# winrt

I have WriteableBitmap. I know that in variable wp1 exists image, because I saved this picture, and it was all fine. I need to encode the image into a byte[] array.

WriteableBitmap wp1 = new WriteableBitmap(1, 1); ;
wp1.SetSourceAsync(memStream);

using (Stream stream = wp1.PixelBuffer.AsStream())
{
    if (stream.CanWrite)
    {
        byte[] pixelArray = new byte[stream.Length];
        await stream.ReadAsync(pixelArray, 0, pixelArray.Length);
    }
}

After all the pixelArray is empty. Length of the array pixelArray equals to the length of stream, but all bytes are zero. What should I do?

Upvotes: 0

Views: 305

Answers (2)

VasileF
VasileF

Reputation: 2916

If you're using WriteableBitmap Extensions, there's an easier method that does that for you, something like :var pixelDataArray = wp1.ToByteArray();

Upvotes: 0

K Mehta
K Mehta

Reputation: 10553

I think your problem is this line of code:

wp1.SetSourceAsync(memStream);

That's an asynchronous method, so you'll have to wait until it's done before proceeding. Try changing it to:

await wp1.SetSourceAsync(memStream);

Upvotes: 2

Related Questions