Skip to content

Commit

Permalink
Fix issues#13. Add image cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
qianlifeng committed Mar 9, 2014
1 parent 8922cc7 commit fe7afe3
Showing 1 changed file with 25 additions and 10 deletions.
35 changes: 25 additions & 10 deletions Wox/ImagePathConverter.cs
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
Expand All @@ -11,43 +12,57 @@ namespace Wox
{
public class ImagePathConverter : IMultiValueConverter
{
private static Dictionary<string, object> imageCache = new Dictionary<string, object>();

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);
}
else if (!string.IsNullOrEmpty(resolvedPath) && File.Exists(resolvedPath))
{
img = new BitmapImage(new Uri(resolvedPath));
}

if (!string.IsNullOrEmpty(resolvedPath) && File.Exists(resolvedPath))
if (img != null)
{
return new BitmapImage(new Uri(resolvedPath));
imageCache.Add(fullPath, img);
}

return null;
return img;
}

public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
Expand Down

0 comments on commit fe7afe3

Please sign in to comment.