繁体   English   中英

WP7的WriteableBitmap图像IValueConverter的字节数组

[英]byte array to WriteableBitmap image IValueConverter for WP7

我解决这个问题已有一段时间了。 我以字节[]的形式从数据库中获取图像,我想将其转换为WritableBitmap,以便可以使用绑定将其显示在我的xaml页面上。

我正在使用这个:

 public class ImageConverter : IValueConverter
{
    /// <summary>
    /// Converts a Jpeg byte array into a WriteableBitmap
    /// </summary>
    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 = new WriteableBitmap(200, 200);
            bmp.LoadJpeg(stream);
            return bmp;
        }
        else
            return null;
    }
    /// <summary>
    /// Converts a WriteableBitmap into a Jpeg byte array.
    /// </summary>
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

第一个问题是它不起作用。 击中bmp.LoadJpeg(stream);时,它将引发未指定的异常bmp.LoadJpeg(stream);

第二个问题是关于传递给WriteableBitmap构造函数的固定大小,我如何知道来自db的照片的大小? 我可以以某种方式使其动态吗? 我猜第二个问题是第一个问题的原因。

谢谢。

编辑

我也尝试使用PictureDecoder.DecodeJpeg()这样:

            MemoryStream stream = new MemoryStream((Byte[])value);
            WriteableBitmap bmp = PictureDecoder.DecodeJpeg(stream);
            return bmp;

但它也不起作用。 在这种情况下, PictureDecoder.DecodeJpeg应该为我创建bmp对象。 我仍然收到未指定的错误。 是我通过了流允许的最大长度吗?

我用这个,但是它返回BitmapImage 您是否需要返回WriteableBitmap

编辑:如注释中提到的Ritch,如果确实需要返回WriteableBitmap添加

var writeableBitmap = new WriteableBitmap(bitmapImage);
return writeableBitmap

第二个问题是关于传递给WriteableBitmap构造函数的固定大小,我如何知道来自db的照片的大小?

创建BitmapImage之后,您就可以访问bitmapImage.PixelWidthbitmapImage.PixelHeight

 public class ByteArraytoImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;

            var byteBlob = value as byte[];
            var ms = new MemoryStream(byteBlob);
            var bmi = new BitmapImage();
            bmi.SetSource(ms);
            return bmi;
        }

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

感谢您的回答

看来问题在于来自数据库的流以某种方式损坏了。 值转换器实际上还可以。 我将其更改为使用PictureDecoder.DecodeJpeg()代替,因此它将更加干净和动态

public class ImageConverter : IValueConverter
{
/// <summary>
/// Converts a Jpeg byte array into a WriteableBitmap
/// </summary>
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;
}
/// <summary>
/// Converts a WriteableBitmap into a Jpeg byte array.
/// </summary>
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
    throw new NotImplementedException();
}
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM