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

匆匆过客 提交于 2019-12-07 01:00:35

问题


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);

回答1:


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.



来源:https://stackoverflow.com/questions/27436237/load-show-convert-image-from-byte-array-database-in-windows-phone-8-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!