posts - 916, comments - 758, trackbacks - 11

My Links



Post Categories

Misc. Coding

WPF: Byte Array to Bitmap Value Converter

I wrote this for use in a little project but as I'm no longer using it, I though I'd stick it on here for anyone who wants it. Essentially, this allows you to take a byte Array (in my case, in the File property of my ThumbnailViewModel) and get it back as a BitmapImage for use in DataBinding...

It also has the property of accepting a parameter which lets you specify the size of thumbnail to use (ThumbSize). Here, I actually have multiple ThumbNails (in the List<ThumbNailViewModel>) and select one using the parameter.

Given this resource string and XAML,

    <local:BytesToBitmapConverter x:Key="bytesToBitmapConverter"/>
<Image Name="IconImage" Source="{Binding Path=ThumbNails, Converter={StaticResource bytesToBitmapConverter}, ConverterParameter=Small}"/>


    public class BytesToBitmapConverter : IValueConverter
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            var thumbs = value as List<ThumbnailViewModel>;
            var param = parameter as String;
            var thisThumb = thumbs.Find(x => x.ThumbnailSize == (ThumbSize)Enum.Parse(typeof(ThumbSize),param));
            BitmapImage bi = new BitmapImage();
            bi.CacheOption = BitmapCacheOption.OnLoad;
            bi.StreamSource = new MemoryStream(thisThumb.File);
            return bi;
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            throw new Exception("The method or operation is not implemented.");

Print | posted on Saturday, February 13, 2010 3:10 PM | Filed Under [ Code Snippets XAML WPF ]

Comments have been closed on this topic.

Powered by: