Byte Array To BitmapImage Converter For Windows Phone Silverlight

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

Download Sample

Byte Array To BitmapImage Converter For Windows Phone Silverlight

15 thoughts on “Byte Array To BitmapImage Converter For Windows Phone Silverlight

    1. 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

  1. 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.”);
    }

    }

  2. 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;
    }

  3. Hi,
    how can i get contact id from windows phone contact table for a particular contact.is there any solution for this .

  4. Hi,sorry. i am new here. Isit this article is for convert byte into image to window phone?

Leave a Reply to Jebarson Cancel reply

Scroll to top
%d bloggers like this: