Example #1
0
        internal static ImageSource GetSmallImage(string filename, bool useCache)
        {
            string extension = Path.GetExtension(filename).ToLower();

            if (useCache)
            {
                if (_Cache.ContainsKey(filename))
                {
                    return(_Cache[filename]);
                }

                if (_Cache.ContainsKey(extension))
                {
                    return(_Cache[extension]);
                }

                FileAttributes attributes = File.GetAttributes(filename);
                if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
                {
                    if (!ShortcutHelper.IsShortcut(filename))
                    {
                        if (_Cache.ContainsKey("!!!"))
                        {
                            return(_Cache["!!!"]);
                        }
                        else
                        {
                            ImageSource folderImage = GetFolderImage(false);
                            _Cache.Add("!!!", folderImage);
                            return(folderImage);
                        }
                    }
                }
            }

            IntPtr     hImgSmall;         //the handle to the system image list
            SHFILEINFO shinfo = new SHFILEINFO();

            //Use this to get the small Icon
            hImgSmall = Win32.SHGetFileInfo(filename, Win32.FILE_ATTRIBUTE_NORMAL, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_SMALLICON | Win32.SHGFI_USEFILEATTRIBUTES);

            ImageSource image = GetImage(shinfo.hIcon);

            Win32.DestroyIcon(hImgSmall);

            if (useCache)
            {
                if (extension == ".exe" || extension == ".ico" || extension == string.Empty || ShortcutHelper.IsShortcut(filename))
                {
                    _Cache.Add(filename, image);
                }
                else
                {
                    _Cache.Add(extension, image);
                }
            }

            return(image);
        }
Example #2
0
        internal static ImageSource GetImage(string filename, bool useCache)
        {
            string extension = Path.GetExtension(filename).ToLower();

            if (useCache)
            {
                if (_Cache.ContainsKey(filename))
                {
                    return(_Cache[filename]);
                }
                if (_Cache.ContainsKey(extension))
                {
                    return(_Cache[extension]);
                }

                FileAttributes attributes = File.GetAttributes(filename);
                if ((attributes & FileAttributes.Directory) == FileAttributes.Directory)
                {
                    if (!ShortcutHelper.IsShortcut(filename))
                    {
                        if (_Cache.ContainsKey("!!!"))
                        {
                            return(_Cache["!!!"]);
                        }
                        else
                        {
                            ImageSource folderImage = GetFolderImage(false);
                            _Cache.Add("!!!", folderImage);
                            return(folderImage);
                        }
                    }
                }
            }

            SHFILEINFO shinfo = new SHFILEINFO();

            IntPtr      ptrIconList = Win32.SHGetFileInfo(filename, Win32.FILE_ATTRIBUTE_NORMAL, ref shinfo, (uint)Marshal.SizeOf(shinfo), Win32.SHGFI_SYSICONINDEX | Win32.SHGFI_SMALLICON | Win32.SHGFI_USEFILEATTRIBUTES);
            IntPtr      ptrIcon     = Win32.ImageList_GetIcon(ptrIconList, shinfo.iIcon.ToInt32(), Win32.ILD_TRANSPARENT);
            ImageSource image       = GetImage(ptrIcon);

            Win32.DestroyIcon(ptrIcon);
            Win32.DestroyIcon(ptrIconList);

            if (useCache)
            {
                if (extension == ".exe" || extension == ".ico" || extension == string.Empty || ShortcutHelper.IsShortcut(filename))
                {
                    _Cache.Add(filename, image);
                }
                else
                {
                    _Cache.Add(extension, image);
                }
            }

            return(image);
        }