Exemplo n.º 1
0
        public static Icon GetIconFrom(string PathName, IconSize IcoSize, bool UseFileAttributes)
        {
            Icon        ico    = null;
            SHFILEINFOW shinfo = new SHFILEINFOW();
            uint        flags  = SHGFI_SYSICONINDEX;

            if (UseFileAttributes)
            {
                flags |= SHGFI_USEFILEATTRIBUTES;
            }

            if (SHGetFileInfoW(PathName, FILE_ATTRIBUTE_NORMAL, ref shinfo, Marshal.SizeOf(shinfo), flags) == 0)
            {
                throw new System.IO.FileNotFoundException();
            }

            Guid       iidImageList = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950");
            IImageList iml          = null;

            SHGetImageList((int)IcoSize, ref iidImageList, ref iml);

            if (iml != null)
            {
                IntPtr hIcon = IntPtr.Zero;
                iml.GetIcon(shinfo.iIcon, (int)ILD_IMAGE, ref hIcon);
                ico = (Icon)Icon.FromHandle(hIcon).Clone();
                DestroyIcon(hIcon);

                if (ico.ToBitmap().PixelFormat != PixelFormat.Format32bppArgb)
                {
                    ico.Dispose();
                    iml.GetIcon(shinfo.iIcon, (int)ILD_TRANSPARENT, ref hIcon);
                    ico = (Icon)Icon.FromHandle(hIcon).Clone();
                    DestroyIcon(hIcon);
                }
            }

            return(ico);
        }
Exemplo n.º 2
0
        public static bool GetFileInfo(string fileName, ref Item item)
        {
            if (!File.Exists(fileName))
            {
                return(false);
            }

            var info = new SHFILEINFOW();

            SHGetFileInfoW(fileName, FileAttributesFlags.FILE_ATTRIBUTE_NORMAL,
                           ref info, (uint)Marshal.SizeOf(info),
                           ShellFileInfoFlags.SHGFI_ICON | ShellFileInfoFlags.SHGFI_SMALLICON |
                           ShellFileInfoFlags.SHGFI_TYPENAME | ShellFileInfoFlags.SHGFI_DISPLAYNAME);

            item.Image = Imaging.CreateBitmapSourceFromHIcon(info.hIcon, Int32Rect.Empty,
                                                             BitmapSizeOptions.FromEmptyOptions());
            item.Type   = info.szTypeName;
            item.Title  = Path.GetFileNameWithoutExtension(info.szDisplayName);
            item.Path   = fileName;
            item.Base64 = item.Image.ToBase64String();

            return(true);
        }
Exemplo n.º 3
0
        public static BitmapSource GetIcon(string path, IconSize size)
        {
            BitmapSource result = null;

            var shinfo = new SHFILEINFOW();
            var flags  = SHGFI_SYSICONINDEX;

            if (SHGetFileInfoW(path, FILE_ATTRIBUTE_NORMAL, ref shinfo, Marshal.SizeOf(shinfo), flags) != 0)
            {
                IImageList iml  = null;
                var        guid = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950");
                SHGetImageList((int)size, ref guid, ref iml);

                if (iml != null)
                {
                    var hIcon = IntPtr.Zero;
                    iml.GetIcon(shinfo.iIcon, (int)ILD_IMAGE, ref hIcon);

                    var icon = Icon.FromHandle(hIcon);

                    if (icon.ToBitmap().PixelFormat != PixelFormat.Format32bppArgb)
                    {
                        icon.Dispose();
                        DestroyIcon(hIcon);
                        iml.GetIcon(shinfo.iIcon, (int)ILD_TRANSPARENT, ref hIcon);
                    }

                    result = Imaging.CreateBitmapSourceFromHIcon(icon.Handle, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
                    result.Freeze();

                    icon.Dispose();
                    DestroyIcon(hIcon);
                }
            }

            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Extracts an icon from any file type.
        /// </summary>
        public static Icon ExtractIcon(string filePath, IconSize size)
        {
            Icon icon     = null;
            var  fileInfo = new SHFILEINFOW();

            if (NativeMethods.SHGetFileInfoW(filePath, NativeMethods.FILE_ATTRIBUTE_NORMAL, ref fileInfo, Marshal.SizeOf(fileInfo), NativeMethods.SHGFI_SYSICONINDEX) == 0)
            {
                throw new FileNotFoundException();
            }

            var        iidImageList = new Guid("46EB5926-582E-4017-9FDF-E8998DAA0950");
            IImageList imageList    = null;

            NativeMethods.SHGetImageList((int)size, ref iidImageList, ref imageList);

            if (imageList != null)
            {
                var hIcon = IntPtr.Zero;
                imageList.GetIcon(fileInfo.iIcon, (int)NativeMethods.ILD_IMAGE, ref hIcon);
                icon = Icon.FromHandle(hIcon).Clone() as Icon;

                NativeMethods.DestroyIcon(hIcon);

                if (icon.ToBitmap().PixelFormat != PixelFormat.Format32bppArgb)
                {
                    icon.Dispose();

                    imageList.GetIcon(fileInfo.iIcon, (int)NativeMethods.ILD_TRANSPARENT, ref hIcon);
                    icon = Icon.FromHandle(hIcon).Clone() as Icon;

                    NativeMethods.DestroyIcon(hIcon);
                }
            }

            return(icon);
        }
Exemplo n.º 5
0
 private static extern int SHGetFileInfoW(IntPtr pszPath, uint dwFileAttributes, ref SHFILEINFOW psfi, int cbFileInfo, uint uFlags);
Exemplo n.º 6
0
 private static extern int SHGetFileInfoW([MarshalAs(UnmanagedType.LPWStr)] string pszPath, uint dwFileAttributes, ref SHFILEINFOW psfi, int cbFileInfo, uint uFlags);
Exemplo n.º 7
0
 public static extern IntPtr SHGetFileInfo(string path, uint fileAttributes, ref SHFILEINFOW sfi, uint cbFileInfo, SHGFI flags);
Exemplo n.º 8
0
 private static extern IntPtr SHGetFileInfoW(string pszPath, FileAttributesFlags dwFileAttributes, ref SHFILEINFOW psfi, uint cbSizeFileInfo, ShellFileInfoFlags uFlags);