Beispiel #1
0
        public static BitmapSource GetSmallIcon(string FileName, bool small)
        {
            var  shinfo = new II.SHFILEINFO();
            uint flags;

            if (small)
            {
                flags = II.SHGFI_ICON | II.SHGFI_SMALLICON;
            }
            else
            {
                flags = II.SHGFI_ICON | II.SHGFI_LARGEICON;
            }

            var res = II.SHGetFileInfo(FileName, 0, ref shinfo, Marshal.SizeOf(shinfo), flags);

            if (res == 0)
            {
                throw (new FileNotFoundException());
            }

            var bs = IconSource(shinfo.hIcon);

            bs.Freeze(); // very important to avoid memory leak
            II.DestroyIcon(shinfo.hIcon);

            return(bs);
        }
Beispiel #2
0
        public static BitmapSource GetLargeIcon(string path, bool jumbo)
        {
            var        shinfo                = new II.SHFILEINFO();
            const uint SHGFI_SYSICONINDEX    = 0x4000;
            const int  FILE_ATTRIBUTE_NORMAL = 0x80;
            var        flags = SHGFI_SYSICONINDEX;

            var res = II.SHGetFileInfo(path, FILE_ATTRIBUTE_NORMAL, ref shinfo, Marshal.SizeOf(shinfo), flags);

            if (res == 0)
            {
                return(null);
            }

            var iconIndex = shinfo.iIcon;

            // Get the System IImageList object from the Shell:
            var iidImageList = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950");

            II.IImageList iml;
            var           size = jumbo ? II.SHIL_JUMBO : II.SHIL_EXTRALARGE;

            II.SHGetImageList(size, ref iidImageList, out iml);
            var       hIcon           = IntPtr.Zero;
            const int ILD_TRANSPARENT = 1;

            iml.GetIcon(iconIndex, ILD_TRANSPARENT, ref hIcon);

            var bs = IconSource(hIcon);

            bs.Freeze(); // very important to avoid memory leak
            II.DestroyIcon(hIcon);
            II.SendMessage(hIcon, II.WM_CLOSE, IntPtr.Zero, IntPtr.Zero);

            return(bs);
        }