0

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?

1
  • Why are you checking for the ability to write to the stream before reading from it? It's not really clear what you're trying to do here. I strongly suspect that SetSourceAsync isn't what you want - and I notice you're not awaiting it, either. Commented Apr 7, 2013 at 8:47

2 Answers 2

2

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.