Windows Phone 7 Silverlight binding image from the IsolatedStorage

左心房为你撑大大i 提交于 2019-11-29 09:00:25

First, create this converter:

public class BinaryToImageSourceConverter : IValueConverter
{

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value != null && value is byte[])
        {
            var bytes = value as byte[];
            var stream = new MemoryStream(bytes);
            var image = new BitmapImage();

            image.SetSource(stream);
            stream.Close();
            return image;
        }
        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Second, bind to Your byte[] using this converter, i.e. if you are using MVVM: View:

<Image Source="{Binding IsolatedStorageImage, Converter={StaticResource BinaryToImageSourceConverter}}" x:Name="ScanImage"/>

You can make property in contrlol (prop snippet) type byte[] and read image to byte array from isostorage, then set value of property to it. If You got more questions, feel free to ask me.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!