[英]byte array to WriteableBitmap image IValueConverter for WP7
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.
我以字节[]的形式从数据库中获取图像,我想将其转换为WritableBitmap,以便可以使用绑定将其显示在我的xaml页面上。
I am using this: 我正在使用这个:
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();
}
}
The first problem is that it doesn't work. 第一个问题是它不起作用。 it throws an unspecified exception when it hits
bmp.LoadJpeg(stream);
击中
bmp.LoadJpeg(stream);
时,它将引发未指定的异常bmp.LoadJpeg(stream);
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 ? 第二个问题是关于传递给WriteableBitmap构造函数的固定大小,我如何知道来自db的照片的大小? can i make it dynamic somehow ?
我可以以某种方式使其动态吗? I guess the second problem is the cause of the first one.
我猜第二个问题是第一个问题的原因。
Thanks. 谢谢。
EDIT 编辑
I have also tries using PictureDecoder.DecodeJpeg()
like this: 我也尝试使用
PictureDecoder.DecodeJpeg()
这样:
MemoryStream stream = new MemoryStream((Byte[])value);
WriteableBitmap bmp = PictureDecoder.DecodeJpeg(stream);
return bmp;
but it didn't work either. 但它也不起作用。 in this case
PictureDecoder.DecodeJpeg
suppose to create the bmp object for me. 在这种情况下,
PictureDecoder.DecodeJpeg
应该为我创建bmp对象。 I still get an unspecified error. 我仍然收到未指定的错误。 could it be that i passed the maximum length allowed for the stream ?
是我通过了流允许的最大长度吗?
I use this but it returns BitmapImage
. 我用这个,但是它返回
BitmapImage
。 Do you need WriteableBitmap
returned? 您是否需要返回
WriteableBitmap
?
edit: as Ritch mentioned in the comments if you do need to return WriteableBitmap
add 编辑:如注释中提到的Ritch,如果确实需要返回
WriteableBitmap
添加
var writeableBitmap = new WriteableBitmap(bitmapImage);
return 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 ?
第二个问题是关于传递给WriteableBitmap构造函数的固定大小,我如何知道来自db的照片的大小?
Once the BitmapImage is created you have access to bitmapImage.PixelWidth
and bitmapImage.PixelHeight
. 创建BitmapImage之后,您就可以访问
bitmapImage.PixelWidth
和bitmapImage.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();
}
}
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 我将其更改为使用
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.