Feb 15 2012

Byte Array To BitmapImage Converter For Windows Phone Silverlight

Category: .net,Silverlight,Windows PhoneJebarson Jebamony @ 4:18 pm

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

15 Responses to “Byte Array To BitmapImage Converter For Windows Phone Silverlight”

  1. Byte Array To BitmapImage Converter For Windows Phone Silverlight says:

    [...] Read original post at Jebarson’s Site For Microsoft Programmers [...]

  2. Rozy says:

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

    }

  3. F2006 says:

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

  4. Sia malik says:

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

  5. Randy says:

    Hi i have a issue, i keep getting black image. can help me?

  6. hueikar says:

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

  7. hueikar says:

    Hi, do you have the example source code?

Leave a Reply