diff --git a/Wox/ImagePathConverter.cs b/Wox/ImagePathConverter.cs index 3887ca207d..38ade44557 100644 --- a/Wox/ImagePathConverter.cs +++ b/Wox/ImagePathConverter.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Drawing; using System.Globalization; using System.IO; @@ -11,43 +12,57 @@ namespace Wox { public class ImagePathConverter : IMultiValueConverter { + private static Dictionary imageCache = new Dictionary(); + private static ImageSource GetIcon(string fileName) { Icon icon = Icon.ExtractAssociatedIcon(fileName); - return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon( - icon.Handle, - new Int32Rect(0, 0, icon.Width, icon.Height), - BitmapSizeOptions.FromEmptyOptions()); + if (icon != null) + { + return System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(icon.Handle, new Int32Rect(0, 0, icon.Width, icon.Height), BitmapSizeOptions.FromEmptyOptions()); + } + + return null; } public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { + object img = null; if (values[0] == null) return null; string path = values[0].ToString(); string pluginDirectory = values[1].ToString(); + string fullPath = Path.Combine(pluginDirectory, path); + if (imageCache.ContainsKey(fullPath)) + { + return imageCache[fullPath]; + } string resolvedPath = string.Empty; if (!string.IsNullOrEmpty(path) && path.Contains(":\\") && File.Exists(path)) { resolvedPath = path; } - else if (!string.IsNullOrEmpty(path) && File.Exists(Path.Combine(pluginDirectory,path))) + else if (!string.IsNullOrEmpty(path) && File.Exists(fullPath)) { - resolvedPath = Path.Combine(pluginDirectory, path); + resolvedPath = fullPath; } if (resolvedPath.ToLower().EndsWith(".exe") || resolvedPath.ToLower().EndsWith(".lnk")) { - return GetIcon(resolvedPath); + img = GetIcon(resolvedPath); } - - if (!string.IsNullOrEmpty(resolvedPath) && File.Exists(resolvedPath)) + else if (!string.IsNullOrEmpty(resolvedPath) && File.Exists(resolvedPath)) { - return new BitmapImage(new Uri(resolvedPath)); + img = new BitmapImage(new Uri(resolvedPath)); } - return null; + if (img != null) + { + imageCache.Add(fullPath, img); + } + + return img; } public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)