Click here to Skip to main content
13,504,758 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


11 bookmarked
Posted 29 Dec 2012

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

, 29 Dec 2012
Rate this:
Please Sign up or sign in to vote.
Directly bind an Image as Image control source


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:

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.StreamSource = ms;
                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:

public Image UserImage
            return _model.UserImage;

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

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


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


About the Author

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

You may also be interested in...


Comments and Discussions

QuestionBest solution for my issue Pin
Member 1016087723-Jul-14 15:27
memberMember 1016087723-Jul-14 15:27 
GeneralMy vote of 5 Pin
Alexandru Dicu11-Sep-13 23:41
memberAlexandru Dicu11-Sep-13 23:41 
GeneralRe: My vote of 5 Pin
Shahare12-Sep-13 1:57
memberShahare12-Sep-13 1:57 
GeneralMy vote of 5 Pin
Shahare26-Jun-13 3:55
memberShahare26-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.180417.1 | Last Updated 29 Dec 2012
Article Copyright 2012 by Shahare
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid