完整的问题是如何在Windows Phone 8.1中显示从数据库加载的图像.图像数据作为字节数组加载(检查 - 加载正常).
通过指定urisource显示图像工作正常.
Image img = new Image();
img.Source = new BitmapImage() {UriSource = new Uri("http://www.example.com/1.jpg") };
rootgrid.Children.Add(img);
Run Code Online (Sandbox Code Playgroud)
但是当字节数组(图像)转换为BitmapImage时 - 没有显示任何内容.到目前为止我发现的唯一例外免费示例是:
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);
Run Code Online (Sandbox Code Playgroud)
但是当时没有显示图片.
Microsoft的文档仅包含通过从内部存储打开文件获得的流中图像加载的示例.但我需要加载图像,即保存在sqlite数据库中.图像数据采用jpeg格式.
编辑:基于freshbm解决方案的工作代码:
public async Task<BitmapImage> ConvertToBitmapImage(byte[] image)
{
BitmapImage bitmapimage = null;
using (InMemoryRandomAccessStream ms = new InMemoryRandomAccessStream())
{ …Run Code Online (Sandbox Code Playgroud)