PowerToys/Wox.Infrastructure/Image/ImageLoader.cs

195 lines
6.6 KiB
C#
Raw Normal View History

2014-07-14 19:03:52 +08:00
using System;
using System.Collections.Concurrent;
2014-07-14 19:03:52 +08:00
using System.IO;
using System.Linq;
2014-07-14 19:03:52 +08:00
using System.Runtime.InteropServices;
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;
using Wox.Infrastructure.Logger;
using Wox.Infrastructure.Storage;
2014-07-14 19:03:52 +08:00
namespace Wox.Infrastructure.Image
2014-07-14 19:03:52 +08:00
{
public static class ImageLoader
2014-07-14 19:03:52 +08:00
{
private static readonly ImageCache ImageCache = new ImageCache();
private static readonly BinaryStorage<ConcurrentDictionary<string, int>> Storage;
2016-08-20 08:02:47 +08:00
2014-07-14 19:03:52 +08:00
private static readonly string[] ImageExtions =
2014-07-14 19:03:52 +08:00
{
".png",
".jpg",
".jpeg",
".gif",
".bmp",
".tiff",
".ico"
};
static ImageLoader()
{
Storage = new BinaryStorage<ConcurrentDictionary<string, int>> ("ImageCache");
ImageCache.Usage = Storage.TryLoad(new ConcurrentDictionary<string, int>());
}
public static void Save()
{
ImageCache.Cleanup();
Storage.Save(ImageCache.Usage);
}
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
{
// 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
{
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
{
return new BitmapImage(new Uri(Constant.ErrorIcon));
2015-01-15 20:47:48 +08:00
}
2014-07-14 19:03:52 +08:00
}
catch (System.Exception e)
{
2016-05-16 00:03:06 +08:00
Log.Exception(e);
return ImageCache[Constant.ErrorIcon];
}
2016-05-04 06:21:03 +08:00
}
2014-07-14 19:03:52 +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 })
{
ImageSource img = new BitmapImage(new Uri(icon));
img.Freeze();
ImageCache[icon] = img;
}
Task.Run(() =>
2014-12-18 19:22:47 +08:00
{
2016-11-30 08:31:31 +08:00
Stopwatch.Normal("Preload images from cache", () =>
{
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($"Preload {ImageCache.Usage.Count} images from cache");
});
2014-12-18 19:22:47 +08:00
}
public static ImageSource Load(string path)
2016-05-04 06:21:03 +08:00
{
ImageSource image;
if (string.IsNullOrEmpty(path))
{
image = ImageCache[Constant.ErrorIcon];
}
else if (ImageCache.ContainsKey(path))
2014-07-14 19:03:52 +08:00
{
image = ImageCache[path];
}
else
{
if (path.StartsWith("data:", StringComparison.OrdinalIgnoreCase))
2015-02-04 23:16:41 +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 06:21:03 +08:00
image = ShellIcon(path);
}
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
{
image = ShellIcon(path);
2016-05-04 06:21:03 +08:00
}
2015-11-02 08:04:05 +08:00
}
else
2015-11-02 08:04:05 +08:00
{
image = ImageCache[Constant.ErrorIcon];
2016-08-20 08:02:47 +08:00
path = Constant.ErrorIcon;
2015-11-02 08:04:05 +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
}
else
2015-02-04 23:16:41 +08:00
{
image = ImageCache[Constant.ErrorIcon];
2016-08-20 08:02:47 +08:00
path = Constant.ErrorIcon;
2015-02-04 23:16:41 +08:00
}
2014-07-14 19:03:52 +08:00
}
ImageCache[path] = image;
image.Freeze();
}
return image;
2014-07-14 19:03:52 +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;
[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);
[DllImport("comctl32.dll")]
private static extern IntPtr ImageList_GetIcon(IntPtr himl, int i, uint flags);
}
2014-07-14 19:03:52 +08:00
}