加载,显示,在Windows Phone的8.1转换图像从字节数组(数据库) [英] Load, show, convert image from byte array (database) in Windows Phone 8.1
问题描述
完整的问题是如何显示的Windows Phone 8.1,从数据库中加载图像。
图像数据加载为字节数组。(选中 - 加载罚款)
通过指定urisource工作正常显示图像。
图片IMG =新的图像();
img.Source =新的BitmapImage(){UriSource =新的URI(http://www.domain.com/1.jpg)};
rootgrid.Children.Add(IMG);
但是,当字节阵列(图像的)转化为的BitmapImage - 显示什么。
唯一例外的自由例如我心中已经发现迄今:
公开的BitmapImage ConvertToBitmapImage(字节[]图片)
{
InMemoryRandomAccessStream RAS =新InMemoryRandomAccessStream();
VAR的BitmapImage =新的BitmapImage();
VAR的MemoryStream =新的MemoryStream(图片);
memoryStream.CopyToAsync(ras.AsStreamForWrite());
bitmapImage.SetSourceAsync(RAS);
返回BitmapImage的;
}图片IMG =新的图像();
img.Source = ConvertToBitmapImage(picturebytearray);
rootgrid.Children.Add(IMG);
但是,没有图所示即可。
微软的文档包含例如只流由从内部存储打开文件获得的图像加载。但我需要加载图像,保存在SQLite数据库。
图像数据为JPEG格式。
编辑:
根据工作code freshbm 解决方案:
公共异步任务<&的BitmapImage GT; ConvertToBitmapImage(字节[]图片)
{
的BitmapImage BitmapImage的= NULL;
使用(InMemoryRandomAccessStream毫秒=新InMemoryRandomAccessStream())
{
使用(DataWriter作家=新DataWriter(ms.GetOutputStreamAt(0)))
{
writer.WriteBytes((字节[])图像);
等待writer.StoreAsync();
}
BitmapImage的=新的BitmapImage();
bitmapimage.SetSource(毫秒);
}
返回BitmapImage的;
}
然后在构造函数中你可以使用:
img.Source = ConvertToBitmapImage(imagebytearray)。结果;
否则
img.Source =等待ConvertToBitmapImage(imagebytearray);
您可以尝试这样的事情来转换字节[]以BitmapImage的:
使用(InMemoryRandomAccessStream毫秒=新InMemoryRandomAccessStream())
{
使用(DataWriter作家=新DataWriter(ms.GetOutputStreamAt(0)))
{
writer.WriteBytes((字节[])fileBytes);
writer.StoreAsync()GetResults()。
}
VAR图像=新的BitmapImage();
image.SetSource(毫秒);
}
发现它这样的:
<一href=\"http://www.$c$cproject.com/Tips/804423/Conversion-between-File-Byte-Stream-BitmapImage-an\">http://www.$c$cproject.com/Tips/804423/Conversion-between-File-Byte-Stream-BitmapImage-an
我用它来读取SQLite数据库的byte [],并将其绑定到图像上的页面。
有关您的code尝试添加等待
的异步功能:
公共异步任务&LT;&的BitmapImage GT; ConvertToBitmapImage(字节[]图片)
{
InMemoryRandomAccessStream RAS =新InMemoryRandomAccessStream();
VAR的BitmapImage =新的BitmapImage();
VAR的MemoryStream =新的MemoryStream(图片);
等待memoryStream.CopyToAsync(ras.AsStreamForWrite());
等待bitmapImage.SetSourceAsync(RAS);
返回BitmapImage的;
}图片IMG =新的图像();
img.Source =等待ConvertToBitmapImage(picturebytearray);
rootgrid.Children.Add(IMG);
我不擅长异步编程,但我认为这是可行的。
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.domain.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.
这篇关于加载,显示,在Windows Phone的8.1转换图像从字节数组(数据库)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!