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

February 16th, 2012 10:06 am
[...] Read original post at Jebarson’s Site For Microsoft Programmers [...]
March 30th, 2012 6:14 am
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
March 30th, 2012 10:53 am
Please post your problem along with your code.
April 2nd, 2012 12:53 pm
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.”);
}
}
April 4th, 2012 10:52 am
Taking this issue offline with Rozy
April 4th, 2012 5:57 pm
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;
}
April 11th, 2012 10:54 am
I think that is why we have “using”. Let me know if you face any memory leak
April 11th, 2012 11:10 am
Would be good to update the example in the post
April 9th, 2012 6:53 am
Hi,
how can i get contact id from windows phone contact table for a particular contact.is there any solution for this .
April 11th, 2012 10:57 am
Please check http://msdn.microsoft.com/en-us/library/hh286416(v=VS.92).aspx
April 27th, 2012 1:29 am
Hi i have a issue, i keep getting black image. can help me?
April 29th, 2012 2:55 pm
please check if the image and the format is ok.
June 12th, 2012 1:53 pm
Hi,sorry. i am new here. Isit this article is for convert byte into image to window phone?
July 6th, 2012 8:15 pm
I have uploaded the sample. Hope it helps
June 12th, 2012 2:05 pm
Hi, do you have the example source code?