// // Copyright (c) PlaceholderCompany. All rights reserved. // namespace SystemTrayMenu.Utilities { using System.Collections.Concurrent; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Threading.Tasks; // from https://www.codeproject.com/Articles/2532/Obtaining-and-managing-file-and-folder-icons-using // added ImageList_GetIcon, IconCache, AddIconOverlay /// /// Provides static methods to read system icons for both folders and files. /// /// /// IconReader.GetFileIcon("c:\\general.xls"); /// public static class IconReader { private static readonly ConcurrentDictionary DictIconCache = new ConcurrentDictionary(); public static void Dispose() { foreach (Icon icon in DictIconCache.Values) { icon?.Dispose(); } } public static Icon GetFileIconWithCache(string filePath) { Icon icon = null; string extension = Path.GetExtension(filePath); if (IsExtensionWitSameIcon(extension)) { icon = DictIconCache.GetOrAdd(extension, GetIcon); Icon GetIcon(string keyExtension) { return GetFileIconSTA(filePath); } } else { icon = GetFileIconSTA(filePath); } return icon; } public static Icon GetFolderIconSTA(string directoryPath) { Task task = Task.Factory.StartNew(() => IconsFromSystemCache.GetIcon(directoryPath)); return task.Result; } public static Icon AddIconOverlay(Icon originalIcon, Icon overlay) { Icon icon = null; if (originalIcon != null) { using Bitmap target = new Bitmap( originalIcon.Width, originalIcon.Height, PixelFormat.Format32bppArgb); Graphics graphics = Graphics.FromImage(target); graphics.DrawIcon(originalIcon, 0, 0); graphics.DrawIcon(overlay, 0, 0); target.MakeTransparent(target.GetPixel(1, 1)); icon = Icon.FromHandle(target.GetHicon()); } return icon; } private static bool IsExtensionWitSameIcon(string fileExtension) { bool isExtensionWitSameIcon = true; List extensionsWithDiffIcons = new List { string.Empty, ".EXE", ".LNK", ".ICO", ".URL" }; if (extensionsWithDiffIcons.Contains(fileExtension.ToUpperInvariant())) { isExtensionWitSameIcon = false; } return isExtensionWitSameIcon; } private static Icon GetFileIconSTA(string filePath) { Task task = Task.Factory.StartNew(() => IconsFromSystemCache.GetIcon(filePath)); return task.Result; } } }