40

I have a wpf application and a class library(dll) project. In my dll I have an images folder with some .png files set as Resource.

I want to reference and display the image using the wpf main application.

Any ideas? I want to do this in the xaml not the code behind if at all possible.

Ta, Matt.

Matt B
  • 741
  • 3
  • 10
  • 17

4 Answers4

64

Assuming you reference the class library from the WPF application you can reference and display the image in the WPF application with the following XAML:

<Image Source="/ClassLibraryName;Component/images/myimage.png"/>

The important thing here is "ClassLibraryName" which is the assembly name for your class library. "/images/myimage.png" is the path to your image.

You can find out more about WPF pack URIs here.

Herohtar
  • 5,347
  • 4
  • 31
  • 41
b-rad
  • 991
  • 8
  • 5
  • 6
    Superb. What does the "Component" have to be in there for? – Matt B Feb 16 '10 at 16:15
  • 5
    Ensure you have the "Build Action" for the resource set to "Resource" if you want to have the image embedded in the assembly. "Content" works as well if you don't want to have your images embedded. – Ashley Davis May 27 '10 at 12:13
  • If you have a Class Library, a WPF application and an MVC application; can both the WPF and MVC access those image files using that same code (considering it is XAML) OR would you have to hard code the directory for the MVC application? – Termato Jan 14 '14 at 19:11
  • I have found this: http://stackoverflow.com/questions/12882628/how-to-share-images-between-wpf-and-asp-net to create a common repository for both applications, but the problem would be using it to upload files. – Termato Jan 14 '14 at 19:39
  • 1
    This works but we have to make sure that the referenced image file has the "Build Action" set to "Resource" – AL - Lil Hunk Mar 08 '17 at 03:37
  • This works for me at build time, but the image isn't updated in the design view. – Reahreic Feb 11 '21 at 15:12
  • Additionally, how can I make it that the referenced image can be selected via the dropdown in properties – Reahreic Feb 11 '21 at 15:17
10

This did not work for me:

<Image Source="/ClassLibraryName;Component/images/myimage.png"/>

But this did:

<Image Source="pack://application:,,/ClassLibraryName;Component/images/myimage.png"/>

I also cleaned and rebuilt prior to this, but only after adding this addition bit were the images retrieved.

prw56
  • 326
  • 2
  • 12
5

I was already using below but still didn't work.

<Image Source="/ClassLibraryName;Component/images/myimage.png"/>

I changed the "Build Action" to "Resource" but still didn't work.

Finally, after cleaning the solution and rebuilding all, it worked!!!

Setup: Microsoft Visual Studio Enterprise 2015, Windows 10 Pro, C#

icernos
  • 395
  • 3
  • 6
1

First In Class Library Project Set Images(actual Images) Build Type To Resources. Give this Class Library Reference to project whenever you want to use images. When u want this images code as Follow for wpf.

<Image Source="pack://application:,,,/YOUR_DLL_FILE_NAME;Component/Images/Splashscreen.png" Stretch="Fill" />

Images Is FOLDER NAME