Solved

How to bind a local image file to an Image control in xaml?

Posted on 2014-07-31
5
1,257 Views
Last Modified: 2014-08-07
First, thank you for your time.

My question is ultimately how to bind a local png file to an Image control on the xaml using the mvvm pattern and binding.  I am confused about whether to use:
1) <Image Source={Binding MyImage} .../>
2) <Image Source={Binding Path=MyImage} .../>
3) <Image>
            <Image.Source>
                  <BitmapImage UriSource="{Binding MyImageUri}" />
            </Image.Source>
     </Image>
4) Should I use my own converter or use Microsoft's ImageSourceConverter.

Will you please enlighten me?  Please show me the appropriate xaml code along with the corresponding c# code that will load the local png file from disk and make it appear in my view.

Thank you again for your time.
0
Comment
Question by:MichaelDavidCarr
  • 3
  • 2
5 Comments
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 40234835
When you say "local", it sounds like you might want to get an image on the actual machine running the Silverlight, and you do not get access to the machine's file system.

If you mean local to your project, it depends on whether you have the image in there as content, embedded, etc.
0
 

Author Comment

by:MichaelDavidCarr
ID: 40235264
Sorry, I didn't give you enough details about the project.  I am creating a Windows desktop application not an in-browser application.  So, I do believe I have access to the file system.

Again, thank you for your time.
0
 
LVL 23

Accepted Solution

by:
Jens Fiederer earned 500 total points
ID: 40235328
Sorry, I should have figured this out from it being WPF rather than Silverlight!   Almost all of my XAML has been Silverlight rather than WPF.

With WPF you can just do

            image.Source = new BitmapImage(new Uri(@"file:///c:/users/jens/pictures/parkour.jpg"));
0
 

Author Comment

by:MichaelDavidCarr
ID: 40235505
Thank you for responding yet again.

I followed your instructions and my image is still not showing.  Here is the code:

xaml:
       <ComboBox ItemsSource="{Binding Bills}" SelectedValue="{Binding BillSelected}" 
       <Image  Source="{Binding BillImageSource" />

c#:
        public ImageSource BillImageSource;

        // When a bill is selected from the combobox, this property is invoked with the bill that was selected. 
        public string BillSelected
        {
            set
            {
                var selectedBillName = value;
                var filename = _repository.GetBillFilenameByShortName(selectedBillName);
                var uri = new Uri(@"file:" + filename);
                var bitmap = new BitmapImage(uri);
                BillImageSource = bitmap;
                OnPropertyChanged("BillImageSource");
	    }
	}

Open in new window


As I step through the code, everything looks okay to me, and yet the png file that corresponds to the filename doesn't show.
What am I doing wrong?

Thank you for your time Jens.
0
 
LVL 23

Expert Comment

by:Jens Fiederer
ID: 40235508
Take a look at "filename"....does it look like this, like in my code?

///c:/users/jens/pictures/parkour.jpg

I'd be kind of surprised if it did, I think you might need to do some string munging to get this right.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now