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);
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);
}
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.
User contributions licensed under CC BY-SA 3.0