Using Image embedded in another dll in a WPF application
In this article we will see how to use an image which is embedded in another dll(probably a part of your another project) in a WPF application. We will also see how to resolve the error:The URI prefix is not recognized Error while setting the Image Source property to the image in another dll file.
In this article we will focus on how to use an image which is embedded in another dll(probably a part of your another project) in a WPF application. We will also see how to resolve the error:The URI prefix is not recognized Error while setting the Image Source property to the image in another dll file.
For demo purpose I have created a webapplication named "WebApplication2" and added an image to this web application. The image name is "heroAccent.png". Right click the image and set its BuildAction to resource. Build the project and it will generate a dll file with the application name in the bin folder of the project.
Now create a New WPF application and write the below code in the .xaml file. This code adds a stackpanel in the Grid.
<StackPanel x:Name="StackPanel1">
Then Add reference to the WebApplication2.dll file in the WPF project.
Next add the below code in the .xaml.cs code-behind file to add an image to the stack panel. In the constructor after the InitializeComponent method invocation write the below code:
Image img1 = new Image();
img1.Source = new BitmapImage(new Uri(@"pack://application:,,,/WebApplication2;component/heroAccent.png"));
StackPanel1.Children.Add(img1);
Here we are referring to the dll "WebApplication2" and the image heroAccent.png which is a embedded in the application assembly.
If you get the error:The URI prefix is not recognized. Please make sure that the image BuildAction is set to "Resource" and the image path is correctly specified in the code.
Here we have used the pack URI syntax to access the image which is embedded in "WebApplication2" assembly.
The pack URI syntax for accessing embedded resources in other assemblies is:
pack://application:,,,/<AssemblyName>;component/<Folder>/<FileName>