Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URI to local file relative to assembly

For example I have the following project structure:

Root\Core\Application.exe
Root\Modules\Assembly.dll
Root\Modules\Icons\Icon.png

My Application.exe loads the Assembly.dll. The Assembly.dll includes a WPF UserControl with an Image control. The Icon.png is not an embedded resource, it is a local file (Build: Content).

I tried every possible URI in the Source-Property of the Image Control, but it never shows the image :-(

<Image Source="Icons\Icon.png" />
<Image Source="pack://application:,,,/Icons\Icon.png" />
<Image Source="pack://application:,,,/Assembly;component/Icons/Icon.png" />

etc.

How does the URI have to look like?

like image 550
Sascha Lange Avatar asked Sep 03 '12 10:09

Sascha Lange


1 Answers

This statement implies you can't refer to the Content files in your Assembly...(so only Content files that were specified in your Application can be resolved using "application://").

http://msdn.microsoft.com/en-us/library/aa970069(v=vs.85).aspx

Content files in referenced assemblies are not included because they are unsupported by WPF. Pack URIs for embedded files in referenced assemblies are unique because they include both the name of the referenced assembly and the ;component suffix. Pack URIs for site of origin files are unique because they use the are the only pack URIs that use the siteoforigin:/// authority.

You might get somewhere with "siteoforigin" which refers to the location where your .exe is running.

Source="pack://siteoforigin:,,,/../Modules/Icons/Icon.png"

or

Source="pack://siteoforigin:,,,/Icons/Icon.png"

Though it might not support the relative path.

Give this a shot as well:

Source="../Modules/Icons/Icon.png" 

And this:

Source="Icons/Icon.png" 

One tip when experimenting with Pack URIs is to build them using the PackUriHelper, so that you conform to the proper syntax/semantics.

See some related posts:

  • Wpf Absolute vs. Relative Pack URIs

  • http://nerddawg.blogspot.co.uk/2005/12/more-on-resource-loading-in-wpf.html


Another idea....

You could define your own MarkupExtension which helped build an Absolute path to your png files.

You would have a global configuration setting which got set just prior to the loading of your DLL with LoadFrom.

Then you would make your XAML use the extension:

Source={local:MyMarkupExtensionPathBuilder Icon.png}

Use this as a starting point...in your case you don't want to use "application://" though...you want to build the Uri as a straight absolute "file" path type URI e.g. "C:\Program Files\myapplication\Root\Modules\Icons\icon.png".

  • http://www.switchonthecode.com/tutorials/wpf-tutorial-fun-with-markup-extensions

You can get your MarkupExtension to return the BitmapSource directly (or just get it to return a string...which then a TypeConverter resolves to a BitmapSource anyway).

(you could also do something similar with a Binding that used a Converter to concatenate the 2 parts of the path, or make your ViewModel do the concatenation if you use one...the markupextension is a neater way to do it though)

like image 102
CSmith Avatar answered Nov 09 '22 23:11

CSmith