Error when setting MemoryStream as the source of a BitMapImage

1

For Windows Phone 8 development, everything I've read is saying that you have to set a stream as the source of a bitmapimage in order to convert a byte[] array to a bitmapimage. When I implement this though, I receive an error at:

 bitmapImage.SetSource(stream);   

Error:

 An exception of type 'System.Exception' occurred in System.Windows.ni.dll 
 but was not handled in user code

 Additional information: The component cannot be found. (Exception from 
 HRESULT: 0x88982F50) 

Code Snippet:

 byte[] bytes = value as byte[];
 MemoryStream stream = new MemoryStream(bytes, 0, bytes.Length);
 BitmapImage bitmapImage = new BitmapImage();
 bitmapImage.SetSource(stream);
c#
windows-phone-8
memorystream
bitmapimage
asked on Stack Overflow Jun 26, 2015 by NathanVigs

2 Answers

1

Often weird errors like this are caused by a failure to set the stream to the starting position. Also, it's good practice to wrap disposable objects in a using statement.

Does this solve the problem?

 var bytes = value as byte[]; 
 using(var stream = new MemoryStream(bytes, 0, bytes.Length))
 {
    //set this to the beginning of the stream
    stream.Position = 0;
    var bitmapImage = new BitmapImage();
    bitmapImage.SetSource(stream);
 }
answered on Stack Overflow Jun 26, 2015 by Colin • edited Jun 26, 2015 by Colin
1

The array you have stored in bytes is not a valid image. You need to go further back to wherever value is getting populated and find out why it is not being populated with the byte array for a image.

answered on Stack Overflow Jun 26, 2015 by Scott Chamberlain

User contributions licensed under CC BY-SA 3.0