Esempio n. 1
0
        protected Bitmap GetGenericIcon(IntPtr ptr, IconSize size, bool isFolder = false, bool forceLoad = false)
        {
            switch (size)
            {
            case IconSize.Thumbnail:
            case IconSize.ExtraLarge:
            case IconSize.Large:
            case IconSize.Jumbo:
                var retImage = GetBitmap(size, ptr, isFolder, forceLoad);
                return(ImageTools.ResizeImage(ImageTools.CheckImage(retImage) ? retImage : ImageTools.CutImage(retImage, new Size(48, 48)), IconSizeToSize(size), 0));
            }

            var shinfo = new NativeMethods.SHFILEINFO();

            var flags = NativeMethods.SHGFI_SYSICONINDEX | NativeMethods.SHGFI_PIDL;

            if (!isFolder)
            {
                flags |= NativeMethods.SHGFI_USEFILEATTRIBUTES;
            }

            if (size == IconSize.Small)
            {
                flags = flags | NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_SMALLICON;
            }
            else
            {
                flags = flags | NativeMethods.SHGFI_ICON;
            }
            try
            {
                NativeMethods.SHGetFileInfo(ptr, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), flags);
            }
            catch
            {
                return(new Bitmap(1, 1));
            }

            if (shinfo.hIcon == IntPtr.Zero)
            {
                return(new Bitmap(1, 1));
            }
            var retVal = Icon.FromHandle(shinfo.hIcon).ToBitmap();

            NativeMethods.DestroyIcon(shinfo.hIcon);
            return(retVal);
        }
Esempio n. 2
0
        protected static Bitmap GetGenericIcon(string fullPathOrExt, IconSize size, bool isFolder = false, bool forceLoad = false)
        {
            try
            {
                var fileName = fullPathOrExt.StartsWith(".") ? "AAA" + fullPathOrExt : fullPathOrExt;

                switch (size)
                {
                case IconSize.Thumbnail:
                case IconSize.ExtraLarge:
                case IconSize.Jumbo:
                    Bitmap retImage = null;

                    try
                    {
                        retImage = GetBitmap(size, fileName, isFolder, forceLoad);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("GetGenericIcon - " + ex.Message);
                        //TO-DO: Fix exception:
                        //GetGenericIcon Unable to cast COM object of type 'System.__ComObject' to interface type 'IImageList'.
                        //This operation failed because the QueryInterface call on the COM component for the interface with IID
                        //'{46EB5926-582E-4017-9FDF-E8998DAA0950}' failed due to the following error: No such interface supported
                        //(Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

                        //FailSafe
                        if (size > IconSize.Large)
                        {
                            return(GetGenericIcon(fullPathOrExt, IconSize.Large, isFolder, forceLoad));
                        }
                    }

                    return(ImageTools.ResizeImage(ImageTools.CheckImage(retImage) ? retImage : ImageTools.CutImage(retImage, new Size(48, 48)), IconSizeToSize(size), 0));
                }

                var shinfo = new NativeMethods.SHFILEINFO();

                var flags = NativeMethods.SHGFI_SYSICONINDEX;
                if (!isFolder)
                {
                    flags |= NativeMethods.SHGFI_USEFILEATTRIBUTES;
                }

                if (size == IconSize.Small)
                {
                    flags = flags | NativeMethods.SHGFI_ICON | NativeMethods.SHGFI_SMALLICON;
                }
                else
                {
                    flags = flags | NativeMethods.SHGFI_ICON;
                }
                try
                {
                    NativeMethods.SHGetFileInfo(fileName, 0, ref shinfo, (uint)Marshal.SizeOf(shinfo), flags);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("GetGenericIcon - " + ex.Message);
                    return(new Bitmap(1, 1));
                }
                if (shinfo.hIcon == IntPtr.Zero)
                {
                    return(new Bitmap(1, 1));
                }
                var retVal = Icon.FromHandle(shinfo.hIcon).ToBitmap();
                NativeMethods.DestroyIcon(shinfo.hIcon);
                return(retVal);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("GetGenericIcon - " + ex.Message);
                return(new Bitmap(1, 1));
            }
        }