Loading an external image via XAML code in WPF?

Mohammad Dayyan picture Mohammad Dayyan · Apr 18, 2010 · Viewed 18.4k times · Source

I have an image lock.png beside of my WPF exe file in the images folder. Now, I'm gonna load it into the WPF Project as an image, I've used the following XAML code:

<Image Stretch="Fill" Source="pack://siteoforigin:,,,/images/lock.png" />

It works, but Expression Blend or Visual Studio doesn't show it when I'm working on the project.
How can we show external images in these situations?

Answer

Hun1Ahpu picture Hun1Ahpu · Apr 18, 2010

Try to load your image dynamically. This should be on xaml:

<Image Stretch="Fill" Name="MyImage" />

And this in code behind. On Window_Loaded or in Window constructor:

if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "images/lock.png"))
            {
                Uri uri = new Uri(AppDomain.CurrentDomain.BaseDirectory + "images/lock.png", UriKind.RelativeOrAbsolute);
                MyImage.Source = BitmapFrame.Create(uri);
            }