Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference images stored in external dll using wpf

Tags:

c#

image

wpf

png

dll

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.

like image 548
Matt B Avatar asked Feb 16 '10 09:02

Matt B


4 Answers

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.

like image 66
b-rad Avatar answered Nov 08 '22 01:11

b-rad


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.

like image 10
prw56 Avatar answered Nov 08 '22 03:11

prw56


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#

like image 5
icernos Avatar answered Nov 08 '22 01:11

icernos


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

like image 1
khushal dawda Avatar answered Nov 08 '22 01:11

khushal dawda