1

I have a WPF application where I added the images as resources to the project (not from project settings, like how the 2nd answer does it here), so they are under a folder. I use them like this in xaml:

<Image Width="32" Height="32" Source="/Images/Effect.png" />

When I run the program manually, either from visual studio or explorer, the images show up fine, no error, etc.

But when I run it from another application via reflection, it prints these exceptions inside visual studio:

System.Windows.Data Error: 6 : 'TargetDefaultValueConverter' converter failed to convert value '/Images/Effect.png' (type 'String'); fallback value will be used, if available. BindingExpression:Path=EffectIcon; DataItem='Node' (HashCode=34743541); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource') IOException:'System.IO.IOException: Cannot locate resource 'images/effect.png'.

In the end, the application starts up fine but all images are missing.

Any idea on how to fix this?

Should I be binding the images in a different way?

Community
  • 1
  • 1
Joan Venge
  • 315,713
  • 212
  • 479
  • 689

2 Answers2

3

The problem is probably that the images will be searched in the assembly from where you instantiated your modules via reflection. One possibility to resolve this problem is, that you assign the full path to the images, including the assembly name. Something like:

Source="/AssemblyName;component/image/effect.png"
HCL
  • 36,053
  • 27
  • 163
  • 213
  • Thanks, what would the assembly name for an exe like LayerView.exe? Also isn't there a some sort of working directory for loading assemblies so it can load it from there? – Joan Venge Apr 27 '11 at 16:27
  • @Joan Venge: Only the name of the assembly, without the .exe-extension. The second question I can not answer, I don't know. – HCL Apr 27 '11 at 16:28
  • I tried `Source="/LayerView.exe;component/image/effect.png"` but it didn't work for the reflection launch. It works on the standard one. – Joan Venge Apr 27 '11 at 16:29
  • I removed `.exe`, same result unfortunately. – Joan Venge Apr 27 '11 at 16:30
  • @Joan Venge: Sad that I could'nt help, I had a similar issue and have it resolved like this, but this seems to be another problem. – HCL Apr 27 '11 at 16:35
  • No worries, still I learnt something new. Probably I need to bind them differently I guess. – Joan Venge Apr 27 '11 at 16:36
1

Try this: "pack://application:,,,/AssemblyName;/components/image/effect.png"

Erich
  • 101
  • 1
  • 5