1

My goal is to upload and download images using a web service. I understand that in order to do that the images need to be converted to a byte array. However, I’m getting “Unspecified error” when converting a byte array into a BitmapImage.

I’ve create a test rig that converts an image (from a PhotoChooserTask) into a byte array and back again that recreates my problem. The code that does the conversion is listed below with the problem line highlighted.

Any help would be appreciated!

private void PhotoChooserTaskCompleted(object sender, PhotoResult e)
{

    if (e.TaskResult == TaskResult.OK)
    {
        //Display the photo
        BitmapImage PhotoBitmap = new BitmapImage();
        PhotoBitmap.SetSource(e.ChosenPhoto);
        Photo.Source = PhotoBitmap;

        //Convert the photo to bytes
        Byte[] PhotoBytes = new byte[e.ChosenPhoto.Length];
        e.ChosenPhoto.Read(PhotoBytes, 0, PhotoBytes.Length);

        //Convert the bytes back to a bitmap
        BitmapImage RestoredBitmap = new BitmapImage();
        MemoryStream stream = new MemoryStream(PhotoBytes);
        BitmapImage image = new BitmapImage();
        RestoredBitmap.SetSource(stream);    //<------ I get "Unspecified error" on this line

        //Display the restored photo
        RestoredPhoto.Source = RestoredBitmap;
    }
}
2
  • Could you check the result of e.ChosenPhoto.Read(PhotoBytes, 0, PhotoBytes.Length);? It should return the number of bytes read. Commented Feb 1, 2012 at 6:13
  • I checked the result of e.ChosenPhoto.Read() and it returns 0 even though e.ChosenPhoto.Length is 119264 - am I missing something when creating the byte array? Commented Feb 1, 2012 at 6:41

4 Answers 4

4

The first time you use e.ChosePhoto as source, the stream is read and the Position property is advanced to the end. You can inspect the PhotoBytes array in the debugger to see that after your read operation it actually does not have any content (or check the return value of the Read method to confirm zero bytes are read).

What you need to do is reset that Position to zero before you read from it again:

//Convert the photo to bytes
Byte[] PhotoBytes = new byte[e.ChosenPhoto.Length];

// rewind first
e.ChosenPhoto.Position = 0;

// now succeeds
e.ChosenPhoto.Read(PhotoBytes, 0, PhotoBytes.Length);
Sign up to request clarification or add additional context in comments.

1 Comment

Correct, i do endorse Mister Goodcat, resetting the Position makes it to workfine to fetch the stream data. :)
0

I would bet that this is what's happening (comments inline):

//Display the photo
BitmapImage PhotoBitmap = new BitmapImage();
PhotoBitmap.SetSource(e.ChosenPhoto); // This is reading from the stream
Photo.Source = PhotoBitmap;

//Convert the photo to bytes
Byte[] PhotoBytes = new byte[e.ChosenPhoto.Length];
e.ChosenPhoto.Read(PhotoBytes, 0, PhotoBytes.Length); // Fails to read the full stream
                                                      // because you already read from it

//Convert the bytes back to a bitmap
BitmapImage RestoredBitmap = new BitmapImage();
MemoryStream stream = new MemoryStream(PhotoBytes); // You're creating a stream that
                                                    // doesn't contain the image
BitmapImage image = new BitmapImage();
RestoredBitmap.SetSource(stream); // Fails because your stream is incomplete

Seek to 0 in the stream before you attempt to read from it. And check the return value from the Read call to make sure it matches PhotoBytes.Length.

Comments

0

This:

//Display the photo
BitmapImage PhotoBitmap = new BitmapImage();
PhotoBitmap.SetSource(e.ChosenPhoto);
Photo.Source = PhotoBitmap;

uses the Stream of the e.ChosenPhoto and might not rewind the Position of the Stream.

So when you do this:

Byte[] PhotoBytes = new byte[e.ChosenPhoto.Length];
e.ChosenPhoto.Read(PhotoBytes, 0, PhotoBytes.Length);

you are starting at the end of the stream reading nothing.

Use Seek to reset the Position of the stream.

Comments

0

Did you check out my other post where I already did this? I had receive a pretty good rating from it.

BitmapImage to byte[] and byte[] to BitmapImage

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.