问题描述
我将图像存储为字节[]数组,因为我不能将它们存储为BitmapImage的。该ShotItem类将被存储在IsolatedStorage在一个ObservableCollection。
I am storing images as byte[] arrays because I can't store them as BitmapImage. The ShotItem class will be stored in IsolatedStorage in an observableCollection.
namespace MyProject.Model
{
public class ShotItem : INotifyPropertyChanged, INotifyPropertyChanging
{
private byte[] _shotImageSource;
public byte[] ShotImageSource
{
get
{
return _shotImageSource;
}
set
{
NotifyPropertyChanging("ShotImageSource");
_shotImageSource = value;
NotifyPropertyChanged("ShotImageSource");
}
}
...
}
}
在我的XAML文件我有以下几点:
In my xaml file I have the following:
<Image Source="{Binding ShotImageSource}" Width="210" Height="158" Margin="12,0,235,0" VerticalAlignment="Top" />
可惜我不能图像作为字节加载直入在XAML图像容器。不知何故,我需要将ShotImageSource的byte []转化为BitmapImage的。我加载了不少图像,以便将这个必须也可以异步完成。
Unfortunately I can't load the image as a byte straight into the Image container in the xaml. I somehow need to convert the ShotImageSource byte[] to BitmapImage. I am loading quite a few images so would this have to also be done asynchronously.
我试图用一个转换器约束力,但我不知道如何获取它的工作。任何帮助将不胜感激。)
I tried to use a converter binding, but I wasn't sure on how to get it to work. Any help would be greatly appreciated :).
推荐答案
下面是一个转换代码
将转换您的字节[]
到的BitmapImage
:
Here is the code for a Converter
that will convert your byte[]
into a BitmapImage
:
public class BytesToImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && value is byte[])
{
byte[] bytes = value as byte[];
MemoryStream stream = new MemoryStream(bytes);
BitmapImage image = new BitmapImage();
image.SetSource(stream);
return image;
}
return null;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
这篇关于的Windows Phone 8 - 负载byte []数组与绑定XAML图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!