Seb*_*123 2 c# windows wpf xaml windows-phone-8
我将图像存储为byte []数组,因为我无法将它们存储为BitmapImage.ShotItem类将存储在IsolatedStorage中的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");
}
}
...
}
}
Run Code Online (Sandbox Code Playgroud)
在我的xaml文件中,我有以下内容:
<Image Source="{Binding ShotImageSource}" Width="210" Height="158" Margin="12,0,235,0" VerticalAlignment="Top" />
Run Code Online (Sandbox Code Playgroud)
不幸的是,我无法将图像作为字节直接加载到xaml中的Image容器中.我不知何故需要将ShotImageSource byte []转换为BitmapImage.我正在加载相当多的图像,所以这也必须异步完成.
我试图使用转换器绑定,但我不知道如何让它工作.任何帮助将不胜感激 :).
下面是一个代码Converter
,将您转换byte[]
成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();
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
5482 次 |
最近记录: |