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?
XAML. WPF image control is a versatile control. We will add the image by using the Syntax: Image Source property. Source Property: From the above example, we have seen that the Source Property is used to define the image we want to display.
XAML is used as the declarative markup language for WPF and Xamarin. Forms. For the most part, the syntax is identical - the primary difference is the objects that are defined/created by the XAML graphs.
XAML is used extensively in Windows Presentation Foundation (WPF), Silverlight, Workflow Foundation (WF), Windows UI Library (WinUI) and Universal Windows Platform (UWP). In WPF and UWP, XAML is a user interface markup language to define UI elements, data binding, and events. In WF, however, XAML defines workflows.
As applied to the . NET Core programming model, XAML simplifies creating a UI for a . NET Core app. You can create visible UI elements in the declarative XAML markup, and then separate the UI definition from the run-time logic by using code-behind files that are joined to the markup through partial class definitions.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With