public static void Initialize() { _storage = new BinaryStorage <ConcurrentDictionary <string, int> > ("Image"); ImageCache.Usage = _storage.TryLoad(new ConcurrentDictionary <string, int>()); foreach (var icon in new[] { Constant.DefaultIcon, Constant.ErrorIcon }) { ImageSource img = new BitmapImage(new Uri(icon)); img.Freeze(); ImageCache[icon] = img; } Task.Run(() => { Stopwatch.Normal("|ImageLoader.Initialize|Preload images cost", () => { ImageCache.Usage.AsParallel().Where(i => !ImageCache.ContainsKey(i.Key)).ForAll(i => { var img = Load(i.Key); if (img != null) { ImageCache[i.Key] = img; } }); }); Log.Info($"|ImageLoader.Initialize|Number of preload images is <{ImageCache.Usage.Count}>"); }); }
public static void PreloadImages() { foreach (var icon in new[] { Constant.DefaultIcon, Constant.ErrorIcon }) { ImageSource img = new BitmapImage(new Uri(icon)); img.Freeze(); ImageCache[icon] = img; } Task.Run(() => { Stopwatch.Normal("|ImageLoader.PreLoadImages|Preload images cost", () => { ImageCache.Usage.AsParallel().Where(i => !ImageCache.ContainsKey(i.Key)).ForAll(i => { var img = Load(i.Key); if (img != null) { ImageCache[i.Key] = img; } }); }); Log.Info($"|ImageLoader.PreLoadImages|Number of preload images is <{ImageCache.Usage.Count}>"); }); }
private static ImageResult LoadInternal(string path, bool loadFullImage = false) { ImageSource image; ImageType type = ImageType.Error; try { if (string.IsNullOrEmpty(path)) { return(new ImageResult(ImageCache[ErrorIconPath], ImageType.Error)); } if (ImageCache.ContainsKey(path)) { return(new ImageResult(ImageCache[path], ImageType.Cache)); } if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase)) { var imageSource = new BitmapImage(new Uri(path)); imageSource.Freeze(); return(new ImageResult(imageSource, ImageType.Data)); } if (!Path.IsPathRooted(path)) { path = Path.Combine(Constant.ProgramDirectory, "Images", Path.GetFileName(path)); } if (Directory.Exists(path)) { /* Directories can also have thumbnails instead of shell icons. * Generating thumbnails for a bunch of folders while scrolling through * results from Everything makes a big impact on performance and * Wox responsibility. * - Solution: just load the icon */ type = ImageType.Folder; image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize, Constant.ThumbnailSize, ThumbnailOptions.IconOnly); } else if (File.Exists(path)) { var extension = Path.GetExtension(path).ToLower(); if (ImageExtensions.Contains(extension)) { type = ImageType.ImageFile; if (loadFullImage) { image = LoadFullImage(path); } else { /* Although the documentation for GetImage on MSDN indicates that * if a thumbnail is available it will return one, this has proved to not * be the case in many situations while testing. * - Solution: explicitly pass the ThumbnailOnly flag */ image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize, Constant.ThumbnailSize, ThumbnailOptions.ThumbnailOnly); } } else { type = ImageType.File; image = WindowsThumbnailProvider.GetThumbnail(path, Constant.ThumbnailSize, Constant.ThumbnailSize, ThumbnailOptions.None); } } else { image = ImageCache[ErrorIconPath]; path = ErrorIconPath; } if (type != ImageType.Error) { image.Freeze(); } } catch (System.Exception e) { Log.Exception($"|ImageLoader.Load|Failed to get thumbnail for {path}", e); type = ImageType.Error; image = ImageCache[ErrorIconPath]; ImageCache[path] = image; } return(new ImageResult(image, type)); }