Follow these steps to read an image file, embedded as a Resource, and display it in the UI:
- First, create a folder named Images inside the project and add an image inside it. Let's name the image image1.png.
- Open the MainWindow.xaml file by navigating to Solution Explorer.
- Add an image tag inside the Grid panel and name it img:
<Grid> <Image x:Name="img" /> </Grid>
- Go to the MainWindow.xaml.cs file and, inside the constructor of the class, just after the InitializeComponent() call, create the streamResourceInfo from the resource stream of the image. Here's the code to get the stream info:
var streamResourceInfo = Application.GetResourceStream(new Uri("Images/image1.png", UriKind.RelativeOrAbsolute));
- Now we need to create the instance of BitmapImage from that stream. Copy the following content and pass the streamResourceInfo.Stream to the StreamSource property of the BitmapImage:
var bitmapImage = new BitmapImage...