While you are binding a byte array of an image file to a Image control in WPF, it displays the image without any problem since, it has the converter for the same defined withing itself. While in Windows Phone, you don’t get the same liberty as with the WPF.
Below is the code which I had for the WPF.
<Image Source="{Binding ImageFileContent}" Height="16" Width="16" />
The object which I bound goes something like below.
[DataContract] [Serializable] public class Photo { [DataMember] public byte[] ImageFileContent { get; set; } [DataMember] public DateTime CreatedOn { get; set; } [DataMember] public PhotoType Type { get; set; } }
The above code will have no problem with WPF but as I said it will not work for Windows Phone. Below is the converter which I wrote which can help you convert the byte array of the file content to a Bitmap so that the Silverlight can bind it.
public class BytesToImageConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { if (value != null && value is byte[]) { byte[] bytes = value as byte[]; MemoryStream stream = new MemoryStream(bytes); BitmapImage image = new BitmapImage(); image.SetSource(stream); return image; } return null; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } }
You will need to bind the Image control as below.
<Image Source="{Binding Image, Converter={StaticResource BytesToImageConverter}}" Height="100" Width="100" />
Considering that you defined the converter class as a static resource.
Edit : Sample added as per developers’ request
it was not work for me .converter in image not called .i cant understand why ..u please help me to sort out of this problem
Please post your problem along with your code.
Heloo i m posting here code of application .u can please rectify this .in which BytesToImageConverter class never worked for me .
IN Xaml, i have this code
in page behind class i have code this ,
public DisplayImage()
{
InitializeComponent();
using (var context = new ContactDataContext(ConnectionString))
{
if (context.DatabaseExists())
{
var personeel = (from rozy in context.PersonnelInfos select rozy.BussinessCard.ToArray()).ToList();
lstImages.Itemsource=personeel ;
}
}
}
}
public class BytesToImageConverter : IValueConverter
{
object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value != null && value is byte[])
{
byte[] ByteArray = value as byte[];
BitmapImage bmp = new BitmapImage();
Stream memo = new MemoryStream(ByteArray);
bmp.SetSource(memo);
return bmp;
}
return null;
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new Exception(“The method or operation is not implemented.”);
}
}
Taking this issue offline with Rozy
The code in the post might cause a possible memory leak. It is a good practice to always dispose a MemoryStream object:
E.g:
byte[] bytes = value as byte[];
Using(MemoryStream stream = new MemoryStream(bytes))
{
BitmapImage image = new BitmapImage();
image.SetSource(stream);
return image;
}
I think that is why we have “using”. Let me know if you face any memory leak
Would be good to update the example in the post 🙂
Hi,
how can i get contact id from windows phone contact table for a particular contact.is there any solution for this .
Please check http://msdn.microsoft.com/en-us/library/hh286416(v=VS.92).aspx
Hi i have a issue, i keep getting black image. can help me?
please check if the image and the format is ok.
Hi,sorry. i am new here. Isit this article is for convert byte into image to window phone?
I have uploaded the sample. Hope it helps
Hi, do you have the example source code?