10

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?

7 Answers 7

5

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); } 
Sign up to request clarification or add additional context in comments.

2 Comments

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.
It may not work for this problem, but it solved my problem of loading external images dynamically... The CurrentDomain.BaseDirectory did the trick for me!!!
4

Use format like: Project;component/ImagePath

e.g.

<Image Source="ImageDemo;component/Images/ISIBAR.png" Name="custLogo"/> 

Where ImageDemo is the project name, Image/ISIBAR.png is the path inside project

Comments

2

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.

6 Comments

Thanks friend, so if I wanna use an external image , I can't work on it in developing time? is it right?
Does the solution I posted not work? that should work for relative URI's ottherwise a fully qualified URI should work fine.
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.
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?
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.
|
1

I had same question.

Make sure image build action is set to Resource. (right click on an image and then go to properties, set build action to resource)

Also, instead of siteoforigin use application authority

source: https://stackoverflow.com/a/18175145/2672788

Comments

1

Could be easier:

<Image x:Name="ImageObject" Source="file:///C:\\1.jpg"/> 

Remember the backslashes!

Comments

0

It is very simple, your image is not displaying because it is not being read by the application after you run it.

A quick way to get around this is by manually dropping the image from the physical folder to the folder in the application. Once it is there, the application will be able to read it.

Comments

-1

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

5 Comments

The image that I've used, is an external image and it's not in Properties.
Maybe adding it into visual studio project solution would help you. Or you dont want to ? Is there some purpose it must remain "external" ?
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.
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 :-(
I have VS2008 SP1 and it doesn't show external images except I run the project.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.