Exemple #1
0
        private static void LoadIconForFile(string path, string key, Action onLoaded = null)
        {
            string ext = System.IO.Path.GetExtension(path).ToLowerInvariant();

            // Don't re-load an icon if we're already trying to load one for that type
            LoadTaskState state;

            if (loadTaskCache.TryGetValue(key, out state))
            {
                // Add notifier
                state.Notifiers.Add(onLoaded);
                return;
            }

            // Spin off a new task to handle loading of the icon
            var task = Task.Factory.StartNew(async() =>
            {
                var icon = await IconUtil.GetIconForPathAsync(
                    path,
                    IconUtil.IconSize.SmallIcon,
                    IconUtil.PathType.File,
                    useCache: false);

                var source = Imaging.CreateBitmapSourceFromHIcon(
                    icon.Handle,
                    //System.Windows.Int32Rect.Empty,
                    new System.Windows.Int32Rect(0, 0, 16, 16), // TODO: This doesn't work well with DPI scaling
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

                // Must freeze the DependencyObject so it can be shared across threads
                source.Freeze();

                // Add the icon to our filetype cache
                // TODO: Do we need to lock for thread safety?
                iconCache[key] = source;

                // Notify users that the icon is ready
                state = loadTaskCache[key];
                state.Notify();
            });

            // Keep track of which filetypes we're currently loading
            loadTaskCache[key] = new LoadTaskState(task, onLoaded);
        }
Exemple #2
0
        public static ImageSource GetIconForFolder()
        {
            // TODO: Make this async also. Probably don't really need to since it'll only be called once.
            if (folderIcon == null)
            {
                var icon = IconUtil.GetIconForPathAsync(
                    "C:\\folderthatdoesntexistasdasfasdfafsd",
                    IconUtil.IconSize.SmallIcon,
                    IconUtil.PathType.Directory
                    ).Result; // Blocking

                folderIcon = Imaging.CreateBitmapSourceFromHIcon(
                    icon.Handle,
                    //System.Windows.Int32Rect.Empty,
                    new System.Windows.Int32Rect(0, 0, 16, 16),
                    System.Windows.Media.Imaging.BitmapSizeOptions.FromEmptyOptions());

                folderIcon.Freeze();
            }

            return(folderIcon);
        }