Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.
How can we show external images in these situations?

like image 428
Mohammad Dayyan Avatar asked Apr 18 '10 12:04

Mohammad Dayyan


People also ask

How do I insert an image in WPF XAML?

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.

Is XAML and WPF same?

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.

How does XAML relate to WPF?

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.

Does XAML work on net framework?

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.


3 Answers

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);
            }
like image 180
Hun1Ahpu Avatar answered Oct 27 '22 00:10

Hun1Ahpu


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

like image 24
Sumon Avatar answered Oct 27 '22 00:10

Sumon


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.

like image 36
Brad Cunningham Avatar answered Oct 27 '22 00:10

Brad Cunningham