views:

486

answers:

3

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?

A: 

Is your primary IDE Visual Studio? If yes, why do this manualy? In Propeties window you can just browse way to image you want to use with your Image component

Xorty
The image that I've used, is an external image and it's not in `Properties`.
Mohammad
Maybe adding it into visual studio project solution would help you.Or you dont want to ? Is there some purpose it must remain "external" ?
Xorty
@Xorty: I wanna use external images because I wanna change them easily whenever I want and without changing the codes. I also wanna decrease the main exe file volume.
Mohammad
I see. I tried using external image from URL and it works here.I tried changing image on URL and pic changed also in Visual Studio.Not sure where your problem might be :-(
Xorty
I have VS2008 SP1 and it doesn't show external images except I run the project.
Mohammad
A: 

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);
            }
Hun1Ahpu
Thanks, But I didn't want it. Furthermore if you tested your code you could see that your solution doesn't work for the problem.
Mohammad
+1  A: 

If the image is relative to your EXE location just do

<Image Source="Images\lock.png" />

If the image isn't relative then you have a larger problem. pack syntax only useful if you are actually "packing" the resource into your assembly.

The problem with loose images and Blend is that Blend hosts your exe in a temp directory that it controls and looks for images relative to that temp directory, which will screw any pathing you are depending on.

Foovanadil
Thanks friend, so if I wanna use an external image , I can't work on it in developing time? is it right?
Mohammad
Does the solution I posted not work? that should work for relative URI's ottherwise a fully qualified URI should work fine.
Foovanadil
Your solution is for situations that we've inserted an image to the project. in my case , I don't wanna insert the image in the project.
Mohammad
What do you mean by "I don't wanna insert the image in the project"? You mean you would like to have the file live on disk as loose content, instead of packed in the assembly as a resource? If the image is on disk as loose content then my solution will work (assuming the image is relative to the path of your application). Maybe I am not understanding your problem fully?
Foovanadil
@Foovanadil: `If the image is on disk as loose content then my solution will work (assuming the image is relative to the path of your application)` No, your solution doesn't work.
Mohammad