字节数组到可写位图图像 IValueConverter for WP7 [英] byte array to WriteableBitmap image IValueConverter for WP7
问题描述
我解决这个问题已经有一段时间了.我从我的数据库中以字节 [] 形式获取图像,我想将其转换为 WritableBitmap,以便我可以使用绑定将其显示在我的 xaml 页面上.
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 page.
我正在使用这个:
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);
The first problem is that it doesn't work. it throws an unspecified exception when it hits bmp.LoadJpeg(stream);
第二个问题是关于传递给 WriteableBitmap 构造函数的固定大小,我怎么知道来自数据库的照片的大小?我可以以某种方式使它动态吗?我猜第二个问题是第一个问题的原因.
The second problem is regarding the fixed size passed to the WriteableBitmap constructor, how can i know the size of the photo that is coming from the db ? can i make it dynamic somehow ? I guess the second problem is the cause of the first one.
谢谢.
编辑
我也尝试过像这样使用 PictureDecoder.DecodeJpeg()
:
I have also tries using PictureDecoder.DecodeJpeg()
like this:
MemoryStream stream = new MemoryStream((Byte[])value);
WriteableBitmap bmp = PictureDecoder.DecodeJpeg(stream);
return bmp;
但它也不起作用.在这种情况下 PictureDecoder.DecodeJpeg
假设为我创建 bmp 对象.我仍然收到未指明的错误.可能是我通过了流允许的最大长度吗?
but it didn't work either. in this case PictureDecoder.DecodeJpeg
suppose to create the bmp object for me. I still get an unspecified error. could it be that i passed the maximum length allowed for the stream ?
推荐答案
感谢您的回答
问题似乎是来自数据库的流以某种方式损坏了.价值转换器实际上没问题.我已将其更改为使用 PictureDecoder.DecodeJpeg()
代替,因此它会更加干净和动态
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
{
/// <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();
}
}
这篇关于字节数组到可写位图图像 IValueConverter for WP7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!