Solved

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

Posted on 2014-07-31
5
1,286 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
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…

920 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

17 Experts available now in Live!

Get 1:1 Help Now