【问题标题】:byte array to WriteableBitmap image IValueConverter for WP7字节数组到WriteableBitmap图像IValueConverter for WP7
【发布时间】:2012-03-30 18:07:16
【问题描述】:

我已经解决这个问题一段时间了。我从我的数据库中以字节 [] 的形式获取图像,我想将其转换为 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);时会抛出一个未指定的异常@

第二个问题是关于传递给 WriteableBitmap 构造函数的固定大小,我如何知道来自数据库的照片的大小?我可以让它动态吗?我猜第二个问题是第一个问题的原因。

谢谢。

编辑

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

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

但它也没有工作。在这种情况下PictureDecoder.DecodeJpeg 假设为我创建 bmp 对象。我仍然收到一个未指定的错误。难道是我通过了流允许的最大长度?

【问题讨论】:

    标签: c# wpf silverlight windows-phone-7


    【解决方案1】:

    我使用它,但它返回BitmapImage。你需要WriteableBitmap返回吗?

    编辑:正如 Ritch 在 cmets 中提到的,如果您确实需要返回 WriteableBitmap 添加

    var writeableBitmap = new WriteableBitmap(bitmapImage);
    return writeableBitmap
    

    第二个问题是关于传递给 WriteableBitmap 构造函数,我怎么知道照片的大小 是从数据库来的吗?

    创建 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();
            }
        }
    

    【讨论】:

    • 只需添加 var wb = new WriteableBitmap(bmi); return wb; 即可返回 WriteableBitmap,尽管这个问题似乎并不需要它。
    【解决方案2】:

    感谢您的回答

    似乎问题在于来自数据库的流以某种方式损坏。价值转换器实际上还可以。我已将其更改为使用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();
    }
    }
    

    【讨论】:

    • 这不包括文件中的原始 alpha 数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-28
    • 1970-01-01
    • 2012-05-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多