2014-07-14 19:03:52 +08:00
|
|
|
|
using System;
|
2016-04-22 06:37:40 +08:00
|
|
|
|
using System.Collections.Concurrent;
|
2014-07-14 19:03:52 +08:00
|
|
|
|
using System.IO;
|
2016-04-26 08:20:10 +08:00
|
|
|
|
using System.Linq;
|
2014-07-14 19:03:52 +08:00
|
|
|
|
using System.Runtime.InteropServices;
|
2016-05-04 04:18:26 +08:00
|
|
|
|
using System.Threading.Tasks;
|
2014-07-14 19:03:52 +08:00
|
|
|
|
using System.Windows;
|
2016-01-07 05:34:42 +08:00
|
|
|
|
using System.Windows.Interop;
|
2014-07-14 19:03:52 +08:00
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using System.Windows.Media.Imaging;
|
2016-05-03 05:37:01 +08:00
|
|
|
|
using Wox.Infrastructure.Logger;
|
2016-04-21 08:53:21 +08:00
|
|
|
|
using Wox.Infrastructure.Storage;
|
2014-07-14 19:03:52 +08:00
|
|
|
|
|
2016-04-26 08:20:10 +08:00
|
|
|
|
namespace Wox.Infrastructure.Image
|
2014-07-14 19:03:52 +08:00
|
|
|
|
{
|
2016-04-26 09:40:23 +08:00
|
|
|
|
public static class ImageLoader
|
2014-07-14 19:03:52 +08:00
|
|
|
|
{
|
2016-05-04 06:21:03 +08:00
|
|
|
|
private static readonly ConcurrentDictionary<string, ImageSource> ImageSources = new ConcurrentDictionary<string, ImageSource>();
|
2016-08-20 08:02:47 +08:00
|
|
|
|
|
2014-07-14 19:03:52 +08:00
|
|
|
|
|
2016-05-04 09:26:19 +08:00
|
|
|
|
private static readonly string[] ImageExtions =
|
2014-07-14 19:03:52 +08:00
|
|
|
|
{
|
|
|
|
|
".png",
|
|
|
|
|
".jpg",
|
|
|
|
|
".jpeg",
|
|
|
|
|
".gif",
|
|
|
|
|
".bmp",
|
|
|
|
|
".tiff",
|
|
|
|
|
".ico"
|
|
|
|
|
};
|
|
|
|
|
|
2016-04-26 09:40:23 +08:00
|
|
|
|
private static readonly ImageCache _cache;
|
|
|
|
|
private static readonly BinaryStorage<ImageCache> _storage;
|
2016-03-28 10:09:57 +08:00
|
|
|
|
|
2016-04-26 09:40:23 +08:00
|
|
|
|
static ImageLoader()
|
2016-03-28 10:09:57 +08:00
|
|
|
|
{
|
2016-04-21 08:53:21 +08:00
|
|
|
|
_storage = new BinaryStorage<ImageCache>();
|
|
|
|
|
_cache = _storage.Load();
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-26 09:40:23 +08:00
|
|
|
|
public static void Save()
|
2016-04-21 08:53:21 +08:00
|
|
|
|
{
|
2016-05-22 12:30:38 +08:00
|
|
|
|
_cache.Cleanup();
|
2016-04-21 08:53:21 +08:00
|
|
|
|
_storage.Save();
|
2016-03-28 10:09:57 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-04 06:21:03 +08:00
|
|
|
|
private static ImageSource ShellIcon(string fileName)
|
2014-07-14 19:03:52 +08:00
|
|
|
|
{
|
2015-01-15 20:47:48 +08:00
|
|
|
|
try
|
2014-07-14 19:03:52 +08:00
|
|
|
|
{
|
2016-08-20 20:10:33 +08:00
|
|
|
|
// http://blogs.msdn.com/b/oldnewthing/archive/2011/01/27/10120844.aspx
|
|
|
|
|
var shfi = new SHFILEINFO();
|
|
|
|
|
var himl = SHGetFileInfo(
|
|
|
|
|
fileName,
|
|
|
|
|
FILE_ATTRIBUTE_NORMAL,
|
|
|
|
|
ref shfi,
|
|
|
|
|
(uint)Marshal.SizeOf(shfi),
|
|
|
|
|
SHGFI_SYSICONINDEX
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if (himl != IntPtr.Zero)
|
2015-01-15 20:47:48 +08:00
|
|
|
|
{
|
2016-08-20 20:10:33 +08:00
|
|
|
|
var hIcon = ImageList_GetIcon(himl, shfi.iIcon, ILD_NORMAL);
|
|
|
|
|
// http://stackoverflow.com/questions/1325625/how-do-i-display-a-windows-file-icon-in-wpf
|
|
|
|
|
var img = Imaging.CreateBitmapSourceFromHIcon(
|
|
|
|
|
hIcon,
|
|
|
|
|
Int32Rect.Empty,
|
|
|
|
|
BitmapSizeOptions.FromEmptyOptions()
|
|
|
|
|
);
|
|
|
|
|
DestroyIcon(hIcon);
|
|
|
|
|
return img;
|
2016-05-04 06:21:03 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-08-20 20:10:33 +08:00
|
|
|
|
return new BitmapImage(new Uri(Constant.ErrorIcon));
|
2015-01-15 20:47:48 +08:00
|
|
|
|
}
|
2014-07-14 19:03:52 +08:00
|
|
|
|
}
|
2016-05-04 04:18:26 +08:00
|
|
|
|
catch (System.Exception e)
|
|
|
|
|
{
|
2016-05-16 00:03:06 +08:00
|
|
|
|
Log.Exception(e);
|
2016-08-20 08:02:47 +08:00
|
|
|
|
return ImageSources[Constant.ErrorIcon];
|
2016-05-04 04:18:26 +08:00
|
|
|
|
}
|
2016-05-04 06:21:03 +08:00
|
|
|
|
}
|
2014-07-14 19:03:52 +08:00
|
|
|
|
|
2016-04-26 09:40:23 +08:00
|
|
|
|
public static void PreloadImages()
|
2014-12-18 19:22:47 +08:00
|
|
|
|
{
|
2016-08-20 08:02:47 +08:00
|
|
|
|
foreach (var icon in new[] { Constant.DefaultIcon, Constant.ErrorIcon })
|
2016-05-04 09:26:19 +08:00
|
|
|
|
{
|
2016-08-20 20:10:33 +08:00
|
|
|
|
ImageSource img = new BitmapImage(new Uri(icon));
|
2016-05-04 09:26:19 +08:00
|
|
|
|
img.Freeze();
|
|
|
|
|
ImageSources[icon] = img;
|
|
|
|
|
}
|
2016-05-06 04:15:13 +08:00
|
|
|
|
Task.Run(() =>
|
2014-12-18 19:22:47 +08:00
|
|
|
|
{
|
2016-11-30 08:31:31 +08:00
|
|
|
|
Stopwatch.Normal("Preload images from cache", () =>
|
2016-05-04 04:18:26 +08:00
|
|
|
|
{
|
2016-05-04 06:21:03 +08:00
|
|
|
|
_cache.TopUsedImages.AsParallel().Where(i => !ImageSources.ContainsKey(i.Key)).ForAll(i =>
|
2016-05-04 04:18:26 +08:00
|
|
|
|
{
|
|
|
|
|
var img = Load(i.Key);
|
|
|
|
|
if (img != null)
|
|
|
|
|
{
|
2016-05-04 06:21:03 +08:00
|
|
|
|
ImageSources[i.Key] = img;
|
2016-05-04 04:18:26 +08:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
2015-11-05 05:35:04 +08:00
|
|
|
|
});
|
2016-05-03 05:37:01 +08:00
|
|
|
|
Log.Info($"Preload {_cache.TopUsedImages.Count} images from cache");
|
2014-12-18 19:22:47 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-04 04:18:26 +08:00
|
|
|
|
public static ImageSource Load(string path)
|
2016-05-04 06:21:03 +08:00
|
|
|
|
{
|
2016-05-04 04:18:26 +08:00
|
|
|
|
ImageSource image;
|
2016-04-26 09:40:23 +08:00
|
|
|
|
if (string.IsNullOrEmpty(path))
|
|
|
|
|
{
|
2016-08-20 08:02:47 +08:00
|
|
|
|
image = ImageSources[Constant.ErrorIcon];
|
|
|
|
|
_cache.Add(Constant.ErrorIcon);
|
2016-04-26 09:40:23 +08:00
|
|
|
|
}
|
2016-05-04 06:21:03 +08:00
|
|
|
|
else if (ImageSources.ContainsKey(path))
|
2014-07-14 19:03:52 +08:00
|
|
|
|
{
|
2016-05-04 06:21:03 +08:00
|
|
|
|
image = ImageSources[path];
|
2016-05-04 06:36:47 +08:00
|
|
|
|
_cache.Add(path);
|
2016-05-04 04:18:26 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
|
2015-02-04 23:16:41 +08:00
|
|
|
|
{
|
2016-05-04 04:18:26 +08:00
|
|
|
|
image = new BitmapImage(new Uri(path));
|
2015-02-04 23:16:41 +08:00
|
|
|
|
}
|
2016-05-04 06:21:03 +08:00
|
|
|
|
else if (Path.IsPathRooted(path))
|
2015-02-04 23:16:41 +08:00
|
|
|
|
{
|
2016-05-04 06:21:03 +08:00
|
|
|
|
if (Directory.Exists(path))
|
2016-05-04 04:18:26 +08:00
|
|
|
|
{
|
2016-05-04 06:21:03 +08:00
|
|
|
|
image = ShellIcon(path);
|
2016-05-04 04:18:26 +08:00
|
|
|
|
}
|
2016-05-04 06:21:03 +08:00
|
|
|
|
else if (File.Exists(path))
|
2015-11-02 08:04:05 +08:00
|
|
|
|
{
|
2016-05-04 06:36:47 +08:00
|
|
|
|
var externsion = Path.GetExtension(path).ToLower();
|
|
|
|
|
if (ImageExtions.Contains(externsion))
|
2016-05-04 06:21:03 +08:00
|
|
|
|
{
|
|
|
|
|
image = new BitmapImage(new Uri(path));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-08-20 20:10:33 +08:00
|
|
|
|
image = ShellIcon(path);
|
2016-05-04 06:21:03 +08:00
|
|
|
|
}
|
2015-11-02 08:04:05 +08:00
|
|
|
|
}
|
2016-05-04 04:18:26 +08:00
|
|
|
|
else
|
2015-11-02 08:04:05 +08:00
|
|
|
|
{
|
2016-08-20 08:02:47 +08:00
|
|
|
|
image = ImageSources[Constant.ErrorIcon];
|
|
|
|
|
path = Constant.ErrorIcon;
|
2015-11-02 08:04:05 +08:00
|
|
|
|
}
|
2016-05-04 04:18:26 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-05-19 02:38:43 +08:00
|
|
|
|
var defaultDirectoryPath = Path.Combine(Constant.ProgramDirectory, "Images", Path.GetFileName(path));
|
2016-05-04 06:21:03 +08:00
|
|
|
|
if (File.Exists(defaultDirectoryPath))
|
2015-11-02 08:04:05 +08:00
|
|
|
|
{
|
2016-05-04 06:21:03 +08:00
|
|
|
|
image = new BitmapImage(new Uri(defaultDirectoryPath));
|
2015-11-02 08:04:05 +08:00
|
|
|
|
}
|
2016-05-04 04:18:26 +08:00
|
|
|
|
else
|
2015-02-04 23:16:41 +08:00
|
|
|
|
{
|
2016-08-20 08:02:47 +08:00
|
|
|
|
image = ImageSources[Constant.ErrorIcon];
|
|
|
|
|
path = Constant.ErrorIcon;
|
2015-02-04 23:16:41 +08:00
|
|
|
|
}
|
2014-07-14 19:03:52 +08:00
|
|
|
|
}
|
2016-05-04 06:36:47 +08:00
|
|
|
|
ImageSources[path] = image;
|
|
|
|
|
_cache.Add(path);
|
2016-05-22 12:30:38 +08:00
|
|
|
|
image.Freeze();
|
2016-05-04 04:18:26 +08:00
|
|
|
|
}
|
2016-04-26 06:20:16 +08:00
|
|
|
|
return image;
|
2014-07-14 19:03:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-20 20:10:33 +08:00
|
|
|
|
private const int NAMESIZE = 80;
|
2014-07-14 19:03:52 +08:00
|
|
|
|
private const int MAX_PATH = 256;
|
|
|
|
|
private const uint SHGFI_SYSICONINDEX = 0x000004000; // get system icon index
|
|
|
|
|
private const uint FILE_ATTRIBUTE_NORMAL = 0x00000080;
|
|
|
|
|
private const uint ILD_NORMAL = 0x00000000;
|
|
|
|
|
|
2016-08-20 20:10:33 +08:00
|
|
|
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
|
|
|
|
private struct SHFILEINFO
|
|
|
|
|
{
|
|
|
|
|
readonly IntPtr hIcon;
|
|
|
|
|
internal readonly int iIcon;
|
|
|
|
|
readonly uint dwAttributes;
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)] readonly string szDisplayName;
|
|
|
|
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = NAMESIZE)] readonly string szTypeName;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[DllImport("Shell32.dll", CharSet = CharSet.Unicode)]
|
|
|
|
|
private static extern IntPtr SHGetFileInfo(string pszPath, uint dwFileAttributes, ref SHFILEINFO psfi, uint cbFileInfo, uint uFlags);
|
2014-07-14 19:03:52 +08:00
|
|
|
|
|
|
|
|
|
[DllImport("User32.dll")]
|
|
|
|
|
private static extern int DestroyIcon(IntPtr hIcon);
|
|
|
|
|
|
2016-08-20 20:10:33 +08:00
|
|
|
|
[DllImport("comctl32.dll")]
|
|
|
|
|
private static extern IntPtr ImageList_GetIcon(IntPtr himl, int i, uint flags);
|
|
|
|
|
}
|
2014-07-14 19:03:52 +08:00
|
|
|
|
}
|