I am trying to load some BitmapImages from files held on the file system. I have a dictionary of keys and relative filepaths. Unfortunately the Uri constructor seems non deterministic in the way that it will load the images.
Here is my code:
foreach(KeyValuePair<string, string> imageLocation in _imageLocations)
{
try
{
BitmapImage img = new BitmapImage();
img.BeginInit();
img.UriSource = new Uri(@imageLocation.Value, UriKind.Relative);
img.EndInit();
_images.Add(imageLocation.Key, img);
}
catch (Exception ex)
{
logger.Error("Error attempting to load image", ex);
}
}
Unfortunately sometimes the Uris get loaded as relative file Uris and sometimes they get loaded as relative Pack Uris. There doesn't seem to be any rhyme or reason as to which will get loaded which way. Sometimes I get all the Uris loading one way, or just a couple, or most of them, and it will change each time I run the code.
Any ideas what is going on here?