Loading an external image via XAML code in WPF?
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.
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);
}
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
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.
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
Could be easier:
<Image x:Name="ImageObject" Source="file:///C:\\1.jpg"/>
Remember the backslashes!
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.
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
精彩评论