Click here to Skip to main content
15,886,067 members
Articles / Desktop Programming / WPF
Tip/Trick

Simple Way to Bind an Image Class as Source to Image Control

Rate me:
Please Sign up or sign in to vote.
4.60/5 (5 votes)
29 Dec 2012CPOL1 min read 56.2K   12   4
Directly bind an Image as Image control source

Introduction

The Image control in WPF takes as source a URL of an image or a BitmapImage which contains a URL within. In this article, I will present a way to bind an Image class object (which does not contain an image URL) directly to an Image control source.

Using the Code

I won't talk about the conversion itself from Image to BitmapImage as we can find samples for it over the web easily (e.g. here). The idea here is to use a converter which will accept as value an Image from the binding, convert it to BitmapImage and return it to the Image control source property.

For this, we need to setup an IValueConverter that will get a value which will be of type Image and convert it to BitmapImage like this:

C#
public class ImageToSourceConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, 
				System.Globalization.CultureInfo culture)
        {
            Image image = value as Image;
            if (image != null)
            {
                MemoryStream ms = new MemoryStream();
                image.Save(ms, image.RawFormat);
                ms.Seek(0, SeekOrigin.Begin);
                BitmapImage bi = new BitmapImage();
                bi.BeginInit();
                bi.StreamSource = ms;
                bi.EndInit();
                return bi;
            }
            return null;
        }

        public object ConvertBack(object value, Type targetType, 
			object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

In the code behind, you will have something like this:

C#
public Image UserImage
{
    get
        {
            return _model.UserImage;
        }
}   

And in the XAML, you will bind them directly like this:

XML
<converters:ImageToSourceConverter x:Name="ImageToSourceConverter"/>
...
<Image source="{Binding Path=UserImage, Converter={StaticResource ImageToSourceConverter}" />   

And by that, the binding is direct between the Image and the source of the Image control.

Points of Interest

At first, I did the simple thing of converting in the viewmodel and then bind the source to an object of type BitmapImage. Once I needed it the second time in another viewmodel, it was clear I needed to do something else, and a converter was the best solution.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Israel Israel
Just a guy who loves to code, and was fortune enough to also work in coding.

Comments and Discussions

 
QuestionBest solution for my issue Pin
Member 1016087723-Jul-14 15:27
Member 1016087723-Jul-14 15:27 
GeneralMy vote of 5 Pin
Alexandru Dicu11-Sep-13 23:41
Alexandru Dicu11-Sep-13 23:41 
GeneralRe: My vote of 5 Pin
Shahare12-Sep-13 1:57
Shahare12-Sep-13 1:57 
GeneralMy vote of 5 Pin
Shahare26-Jun-13 3:55
Shahare26-Jun-13 3:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.