The full question is how to show image loaded from database in windows phone 8.1. Image data is loaded as byte array (checked - loads fine).
Displaying image by specifying urisource works fine.
Image img = new Image();
img.Source = new BitmapImage() {UriSource = new Uri("http://www.example.com/1.jpg") };
rootgrid.Children.Add(img);
But when byte array (of image) is converted to BitmapImage - nothing is shown. The only exception free example I'v found so far is:
public BitmapImage ConvertToBitmapImage(byte[] image)
{
InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
var bitmapImage = new BitmapImage();
var memoryStream = new MemoryStream(image);
memoryStream.CopyToAsync(ras.AsStreamForWrite());
bitmapImage.SetSourceAsync(ras);
return bitmapImage;
}
Image img = new Image();
img.Source = ConvertToBitmapImage(picturebytearray);
rootgrid.Children.Add(img);
But no picture is shown then.
Microsoft's documentation contains example only of image loading from stream that is obtained by opening file from internal storage. But I need to load image, that is saved in sqlite database. Image data is in jpeg format.
Edit: Working code based on freshbm solution:
public async Task<BitmapImage> ConvertToBitmapImage(byte[] image)
{
BitmapImage bitmapimage = null;
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
{
writer.WriteBytes((byte[])image);
await writer.StoreAsync();
}
bitmapimage = new BitmapImage();
bitmapimage.SetSource(ms);
}
return bitmapimage;
}
Then in constructors you can use:
img.Source = ConvertToBitmapImage(imagebytearray).Result;
or else
img.Source = await ConvertToBitmapImage(imagebytearray);
You can try something like this to convert byte[] to BitmapImage:
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{
using (DataWriter writer = new DataWriter(ms.GetOutputStreamAt(0)))
{
writer.WriteBytes((byte[])fileBytes);
writer.StoreAsync().GetResults();
}
var image = new BitmapImage();
image.SetSource(ms);
}
Found it on this: http://www.codeproject.com/Tips/804423/Conversion-between-File-Byte-Stream-BitmapImage-an
I'm using it to read byte[] from sqlite database and bind it to Image on Page.
For your code try adding await
for Async functions:
public async Task<BitmapImage> ConvertToBitmapImage(byte[] image)
{
InMemoryRandomAccessStream ras = new InMemoryRandomAccessStream();
var bitmapImage = new BitmapImage();
var memoryStream = new MemoryStream(image);
await memoryStream.CopyToAsync(ras.AsStreamForWrite());
await bitmapImage.SetSourceAsync(ras);
return bitmapImage;
}
Image img = new Image();
img.Source = await ConvertToBitmapImage(picturebytearray);
rootgrid.Children.Add(img);
I'm not good at Async programming but I think that this would work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With