C# 用于WP7的WriteableBitmap映像IValueConverter的字节数组
我解决这个问题已经有一段时间了。我以字节[]的形式从数据库中获取图像,并希望将其转换为可写位图,以便使用绑定在xaml页面上显示它 我用的是:C# 用于WP7的WriteableBitmap映像IValueConverter的字节数组,c#,wpf,silverlight,windows-phone-7,C#,Wpf,Silverlight,Windows Phone 7,我解决这个问题已经有一段时间了。我以字节[]的形式从数据库中获取图像,并希望将其转换为可写位图,以便使用绑定在xaml页面上显示它 我用的是: public class ImageConverter : IValueConverter { /// <summary> /// Converts a Jpeg byte array into a WriteableBitmap /// </summary> public object Conv
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对象。我仍然得到一个未指明的错误。可能是我通过了流允许的最大长度吗?我使用它,但它返回
BitmapImage
。是否需要返回WriteableBitmap
编辑:如Ritch在评论中所述,如果您确实需要返回WriteableBitmap
add
var writeableBitmap = new WriteableBitmap(bitmapImage);
return writeableBitmap
第二个问题是关于传递给服务器的固定大小
WriteableBitmap构造器,我怎么能知道那张照片的大小
是从db来的吗
创建位图图像后,您可以访问位图图像.PixelWidth
和位图图像.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();
}
}
我使用它,但它返回
位图图像
。是否需要返回WriteableBitmap
编辑:如Ritch在评论中所述,如果您确实需要返回WriteableBitmap
add
var writeableBitmap = new WriteableBitmap(bitmapImage);
return writeableBitmap
第二个问题是关于传递给服务器的固定大小
WriteableBitmap构造器,我怎么能知道那张照片的大小
是从db来的吗
创建位图图像后,您可以访问位图图像.PixelWidth
和位图图像.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();
}
}
谢谢你的回答
问题似乎是来自db的流不知何故被破坏了。值转换器实际上还可以。我将它改为使用PicturedCoder.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();
}
}
公共类图像转换器:IValueConverter
{
///
///将Jpeg字节数组转换为可写位图
///
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
if(值为字节[])
{
MemoryStream stream=新的MemoryStream((字节[])值);
WriteableBitmap bmp=PicturedCoder.DecodeJpeg(流);
返回bmp;
}
其他的
返回null;
}
///
///将可写位图转换为Jpeg字节数组。
///
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}
谢谢你的回答
问题似乎是来自db的流不知何故被破坏了。值转换器实际上还可以。我将它改为使用PicturedCoder.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();
}
}
公共类图像转换器:IValueConverter
{
///
///将Jpeg字节数组转换为可写位图
///
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
if(值为字节[])
{
MemoryStream stream=新的MemoryStream((字节[])值);
WriteableBitmap bmp=PicturedCoder.DecodeJpeg(流);
返回bmp;
}
其他的
返回null;
}
///
///将可写位图转换为Jpeg字节数组。
///
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}
只需添加var wb=new WriteableBitmap(bmi);返回wb
返回一个WriteableBitmap,尽管这个问题似乎并不需要它;返回wb
用于返回WriteableBitmap,尽管此问题似乎并不需要它。这不包括文件中的原始alpha数据。这不包括文件中的原始alpha数据。