static BitmapSource TryLoadImage(ImageSourceInfo img, Size size) { try { var uri = new Uri(img.Uri, UriKind.RelativeOrAbsolute); var info = Application.GetResourceStream(uri); if (info.ContentType.Equals("application/xaml+xml", StringComparison.OrdinalIgnoreCase) || info.ContentType.Equals("application/baml+xml", StringComparison.OrdinalIgnoreCase)) { var component = Application.LoadComponent(uri); if (component is FrameworkElement elem) { return(ResizeElement(elem, size)); } return(null); } else if (info.ContentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase)) { var decoder = BitmapDecoder.Create(info.Stream, BitmapCreateOptions.None, BitmapCacheOption.OnDemand); if (decoder.Frames.Count == 0) { return(null); } return(ResizeImage(decoder.Frames[0], size)); } else { return(null); } } catch { return(null); } }
static BitmapSource TryGetImage(ImageSourceInfo img, Size size) { if (img is null) { return(null); } var key = new ImageKey(img.Uri, size); BitmapSource image; if (imageCache.TryGetValue(key, out var bmp)) { return(bmp); } image = TryLoadImage(img, size); if (image is null) { return(null); } imageCache[key] = image; return(image); }
void InitializeResources() { if (nameToInfosDict != null) { return; } var dict = new Dictionary <string, List <ImageSourceInfo> >(StringComparer.InvariantCultureIgnoreCase); var asmName = assembly.GetName(); var rsrcName = asmName.Name + ".g.resources"; try { var baseUri = "/" + asmName.Name + ";v" + asmName.Version + ";component/"; using (var mod = ModuleDefMD.Load(assembly.ManifestModule)) { var rsrc = mod.Resources.Find(rsrcName) as EmbeddedResource; Debug.Assert(rsrc != null); if (rsrc != null) { var set = ResourceReader.Read(mod, rsrc.Data); foreach (var elem in set.ResourceElements) { const string imagesPrefix = "images/"; if (elem.Name != null && elem.Name.StartsWith(imagesPrefix, StringComparison.OrdinalIgnoreCase)) { var imageName = elem.Name.Substring(imagesPrefix.Length); var nameNoExt = RemoveExtension(imageName); string nameKey = null; ImageSourceInfo?info = null; if (imageName.EndsWith(".xaml", StringComparison.OrdinalIgnoreCase) || imageName.EndsWith(".baml", StringComparison.OrdinalIgnoreCase)) { nameKey = nameNoExt; info = new ImageSourceInfo { Uri = baseUri + RemoveExtension(elem.Name) + ".xaml", Size = ImageSourceInfo.AnySize, }; } else if (imageName.EndsWith(".png", StringComparison.OrdinalIgnoreCase)) { info = new ImageSourceInfo { Uri = baseUri + elem.Name, Size = GetImageSize(nameNoExt, out nameKey) ?? new Size(16, 16), }; } if (info != null && nameKey != null) { List <ImageSourceInfo> list; if (!dict.TryGetValue(nameKey, out list)) { dict.Add(nameKey, list = new List <ImageSourceInfo>()); } list.Add(info.Value); } } } } } } catch { Debug.Fail($"Failed to load resources from {assembly}"); } var dict2 = new Dictionary <string, ImageSourceInfo[]>(dict.Count, StringComparer.InvariantCultureIgnoreCase); foreach (var kv in dict) { dict2[kv.Key] = kv.Value.ToArray(); } nameToInfosDict = dict2; }