byte[] to image convertion in windows phone 7 application
I am doing an application for windows phone 7. the application is access a image from database(Sql server 2008). the data is stored in data type 'image'.I want to Display the image. i use the following code
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
byte[] dat开发者_如何学Ca;
BitmapImage empImage = new BitmapImage();
Stream mm;
data = (byte[])value;
mm = new MemoryStream(data);
mm.Position = 0;
BinaryReader BR = new BinaryReader(mm);
byte[] image=BR.ReadBytes(data.Length);
mm = new MemoryStream(image);
//empImage.SetSource(mm);
return empImage;
}
But there is a 'Unspecified' error at commented line (empImage.SetSource(mm);).
Please help Me......
BitmapImage.SetSource
accepts a Stream
(you can leave out the CreateOptions if you don't need to access the bytes immediately afterwards):
public object Convert(object value, Type targetType, object parameter,
System.Globalization.CultureInfo culture)
{
byte[] data = (byte[])value;
using (MemoryStream stream = new MemoryStream(data))
{
BitmapImage image = new BitmapImage
{
CreateOptions = BitmapCreateOptions.None
};
image.SetSource(stream);
return image;
}
}
Also, I don't think an IValueConverter
is the right place for this sort of code.
And finally, the image
database type has been deprecated in favour of varbinary(MAX)
精彩评论