Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Windows Phone 8-使用绑定将字节[]数组加载到XAML映像中_C#_Wpf_Windows_Xaml_Windows Phone 8 - Fatal编程技术网

C# Windows Phone 8-使用绑定将字节[]数组加载到XAML映像中

C# Windows Phone 8-使用绑定将字节[]数组加载到XAML映像中,c#,wpf,windows,xaml,windows-phone-8,C#,Wpf,Windows,Xaml,Windows Phone 8,我将图像存储为byte[]数组,因为我无法将它们存储为BitmapImage。ShotItem类将存储在observableCollection中的IsolatedStorage中 namespace MyProject.Model { public class ShotItem : INotifyPropertyChanged, INotifyPropertyChanging { private byte[] _shotImageSource;

我将图像存储为byte[]数组,因为我无法将它们存储为BitmapImage。ShotItem类将存储在observableCollection中的IsolatedStorage中

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文件中,我有以下内容:

<Image Source="{Binding ShotImageSource}" Width="210" Height="158" Margin="12,0,235,0" VerticalAlignment="Top" />

不幸的是,我无法将图像作为字节直接加载到xaml中的图像容器中。我需要将ShotImageSource字节[]转换为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();
    }
}

您需要有一个绑定转换器,用于从字节数组创建位图源。如果字节数组包含编码图像(PNG或JPEG),您可以从中创建一个流,并调用
BitmapSource.SetSourceAsync
。如果字节数组是一个原始像素缓冲区,您可以创建一个可写的位图并将像素复制到它的
像素缓冲区
。除了这个转换器,您还应该在XAML中指定它:
如果您使用的是win8.1,它将是异步的,因此memorystream的东西不再适用。解决方案就在这里