Esempio n. 1
0
        public static Icon ExtractAssociatedIcon16_(string fileName)
        {
            var    pos1 = fileName.LastIndexOf(Path.DirectorySeparatorChar);
            var    pos2 = fileName.LastIndexOf('.');
            string ext;

            if (pos2 > pos1)
            {
                ext = fileName.Substring(pos2);
            }
            else
            {
                ext = fileName.Substring(pos1 + 1);
            }
            if (ext.Equals(".ico", StringComparison.InvariantCultureIgnoreCase))
            {
                return(new Icon(fileName, 16, 16));
            }
            try
            {
                using (var key = Registry.ClassesRoot.OpenSubKey(ext))
                {
                    var alias = (string)key.GetValue(null);
                    key.Close();
                    using (var aliasKey = Registry.ClassesRoot.OpenSubKey(alias + @"\DefaultIcon"))
                    {
                        var desc = (string)aliasKey.GetValue(null);
                        var file = desc;
                        var id   = 0;
                        var pos  = desc.LastIndexOf(',');
                        if (pos != -1)
                        {
                            if (int.TryParse(desc.Substring(pos + 1), out id))
                            {
                                file = desc.Substring(0, pos);
                            }
                        }
                        aliasKey.Close();
                        if (file == "%1")
                        {
                            file = fileName;
                        }
                        IntPtr[] icons = new IntPtr[1];
                        var      c     = Shell32.ExtractIconEx(file, id, null, icons, 1);
                        if (c == 1)
                        {
                            return(Icon.FromHandle(icons[0]));
                        }
                        else
                        {
                            return(null);
                        }
                    }
                }
            }
            catch
            {
                return(null);
            }
        }
Esempio n. 2
0
        public static IntPtr GetIconHandle(string filePath)
        {
            if (string.IsNullOrEmpty(filePath))
            {
                return(IntPtr.Zero);
            }

            Shell32.ExtractIconEx(filePath, 0, out IntPtr largeIcon, out IntPtr smallIcon, 1);

            return(largeIcon);
        }
Esempio n. 3
0
        private static BitmapSource?ExtractIconsFromExecutable(ref BitmapSource?smallIcon, ref BitmapSource?largeIcon)
        {
            var executablePath = Assembly.GetExecutingAssembly().Location;
            var phiconLarge    = new[] { IntPtr.Zero };
            var phiconSmall    = new[] { IntPtr.Zero };

            if (Shell32.ExtractIconEx(executablePath.Trim('"'), 0, phiconLarge, phiconSmall, 1) > 0)
            {
                smallIcon = BitmapSourceFromHIcon(phiconSmall[0]);
                largeIcon = BitmapSourceFromHIcon(phiconLarge[0]);
            }
            return(null);
        }
Esempio n. 4
0
        protected System.Drawing.Icon LoadImage(string path)
        {
            try
            {
                if (String.IsNullOrEmpty(path))
                {
                    return(null);
                }

                int      ix     = 0;
                string[] iconIx = path.Split(',');
                if (iconIx.Length > 1 && !String.IsNullOrEmpty(iconIx[1]))
                {
                    if (!int.TryParse(iconIx[1], out ix))
                    {
                        ix = 0;
                    }
                }

                path = Environment.ExpandEnvironmentVariables(iconIx[0]);

                if (!PathHelper.FileExists(path))
                {
                    return(null);
                }

                if (PathHelper.IsPathUNC(path))
                {
                    return(null);
                }

                if (Array.IndexOf(IconOwnExt, Path.GetExtension(path)) >= 0)
                {
                    return(Shell32.ExtractIconEx(path, ix));
                    //return System.Drawing.Icon.ExtractAssociatedIcon(path);
                }
                else
                {
                    //managed = false;
                    return(ShFileInfo.ExtractIcon(path, true));
                }
            }
            catch
            {; }

            return(null);
        }
Esempio n. 5
0
        private static BitmapSource ExtractIconsFromExecutable(ref BitmapSource smallIcon, ref BitmapSource largeIcon)
        {
            var executablePath = System.Windows.Forms.Application.ExecutablePath;
            var phiconLarge    = new[]
            {
                IntPtr.Zero
            };
            var phiconSmall = new[]
            {
                IntPtr.Zero
            };

            if (Shell32.ExtractIconEx(executablePath.Trim('"'), 0, phiconLarge, phiconSmall, 1) <= 0)
            {
                return(null);
            }
            smallIcon = BitmapSourceFromHIcon(phiconSmall[0]);
            largeIcon = BitmapSourceFromHIcon(phiconLarge[0]);
            return(null);
        }
Esempio n. 6
0
        /// <summary>Initializes a new instance of the IconExtractor class.</summary>
        /// <param name="path">The full path to the file containing icons.</param>
        /// <param name="size">An IconSize value indicating whether to retreive 16x16 or 32x32 icons.</param>
        public IconExtractor(string path, IconSize size)
        {
            if (string.IsNullOrEmpty(path))
            {
                throw new ArgumentNullException(nameof(path), @"The icon path cannot be null or empty.");
            }

            if (!File.Exists(path))
            {
                throw new FileNotFoundException(@"The icon file path cannot be found.", path);
            }

            IconSource = path;
            var nullPointers = new IntPtr[0];

            Count = Shell32.ExtractIconEx(path, -1, nullPointers, nullPointers, 1);
            icons = new List <Icon>(Count);

            var largeIcons = size == IconSize.Large ? new IntPtr[Count] : null;
            var smallIcons = size == IconSize.Small ? new IntPtr[Count] : null;

            Shell32.ExtractIconEx(path, 0, largeIcons, smallIcons, Count);
            var extractedIcons = largeIcons ?? smallIcons;

            if ((extractedIcons != null) && (extractedIcons.Length > 0))
            {
                foreach (IntPtr handle in extractedIcons)
                {
                    if (handle != IntPtr.Zero)
                    {
                        icons.Add((Icon)Icon.FromHandle(handle).Clone());
                        User32.DestroyIcon(handle);
                    }
                }
            }
        }
        static public ImageSource FindIconImage(string path, int number)
        {
            string IconKey = CreateIconKeyInfo(path, number);

            if (IconImageDictionary.ContainsKey(IconKey))
            {
                return(IconImageDictionary[IconKey]);
            }
            IntPtr large    = IntPtr.Zero;
            IntPtr small    = IntPtr.Zero;
            int    hSuccess = Shell32.ExtractIconEx(path, number, out large, out small, 1);

            if (hSuccess == 0)
            {
                return(null);
            }
            else
            {
                Icon        appIcon   = Icon.FromHandle(large);
                ImageSource IconImage = IconToImageSource(appIcon);
                IconImageDictionary.Add(IconKey, IconImage);
                return(IconImage);
            }
        }