Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load, show, convert image from byte array (database) in Windows Phone 8.1

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);
like image 789
Artur Alexeev Avatar asked Dec 12 '14 03:12

Artur Alexeev


1 Answers

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.

like image 51
freshbm Avatar answered Oct 20 '22 11:10

freshbm