2

I'm instancing a Uri to be used as an image source but the string (file path) I'm providing is invalid. I understand why - it's because I'm using a relative path:

"Resources/Images/" + draggedAct.Category.ToLower() + ".png"

When I had this problem in asp .net I used Server.MapPath(imageString) to resolve the full path but I don't know the equivalent in WPF.

Thanks a lot,

Dan

crazy_prog
  • 1,085
  • 5
  • 19
  • 34
  • Have you tried using "~/Resources/Images/"? It's another way of doing it that works in asp.net, not sure if it works in WPF though. – Joakim Johansson Dec 21 '11 at 09:48

2 Answers2

4

use the pack uri syntax (see msdn: http://msdn.microsoft.com/en-us/library/aa970069%28v=vs.85%29.aspx):

"pack://application:,,,/Resources/Images/"+ draggedAct.Category.ToLower() +".png"

// Absolute URI (default)
Uri absoluteUri = new Uri("pack://application:,,,/Resources/Images/"+ draggedAct.Category.ToLower() +".png", UriKind.Absolute);

or:

// Relative URI
Uri relativeUri = new Uri("/Resources/Images/"+ draggedAct.Category.ToLower() +".png", UriKind.Relative);
chaosr
  • 494
  • 2
  • 6
  • 17
1

You can use a pack URI, as described in the answer to this StackOverflow Question: Setting WPF image source in code

Community
  • 1
  • 1
Adam
  • 15,537
  • 2
  • 42
  • 63