byte array to WriteableBitmap image IValueConverter for WP7

后端 未结 2 1267
天命终不由人
天命终不由人 2021-01-07 14:12

I have been tackling this problem for some time. I get the image from my DB as a byte[] and i want to convert it to WritableBitmap so i can use binding to show it on my xaml

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-07 14:51

    Thanks for your answer

    It seems that the problem was that the stream coming from the db was corrupted somehow. the value converter was actually okay. i have changed it to use PictureDecoder.DecodeJpeg() instead so it will be more clean and dynamic

    public class ImageConverter : IValueConverter
    {
    /// 
    /// Converts a Jpeg byte array into a WriteableBitmap
    /// 
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (value is byte[])
        {
            MemoryStream stream = new MemoryStream((Byte[])value);
            WriteableBitmap bmp = PictureDecoder.DecodeJpeg(stream);
            return bmp;
        }
        else
            return null;
    }
    /// 
    /// Converts a WriteableBitmap into a Jpeg byte array.
    /// 
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
    }
    

提交回复
热议问题