public static Icon ExtractIconFromFile(string fileAndParam)
 {
     try {
         EmbeddedIconInfo embeddedIcon = GetEmbeddedIconInfo(fileAndParam);
         IntPtr           lIcon        = ExtractIcon(0, embeddedIcon.FileName, embeddedIcon.IconIndex);
         return(Icon.FromHandle(lIcon));
     } catch (Exception) {
         return(null);
         // throw exc;
     }
 }
        protected static EmbeddedIconInfo GetEmbeddedIconInfo(string fileAndParam)
        {
            var embeddedIcon = new EmbeddedIconInfo();

            if (String.IsNullOrEmpty(fileAndParam))
            {
                return(embeddedIcon);
            }

            //Use to store the file contains icon.
            string fileName;

            //The index of the icon in the file.
            int    iconIndex       = 0;
            string iconIndexString = String.Empty;

            int commaIndex = fileAndParam.IndexOf(",");

            //if fileAndParam is some thing likes that: "C:\\Program Files\\NetMeeting\\conf.exe,1".
            if (commaIndex > 0)
            {
                fileName        = fileAndParam.Substring(0, commaIndex);
                iconIndexString = fileAndParam.Substring(commaIndex + 1);
            }
            else
            {
                fileName = fileAndParam;
            }

            if (!String.IsNullOrEmpty(iconIndexString))
            {
                //Get the index of icon.
                iconIndex = int.Parse(iconIndexString);
                if (iconIndex < 0)
                {
                    iconIndex = 0; //To avoid the invalid index.
                }
            }

            embeddedIcon.FileName  = fileName;
            embeddedIcon.IconIndex = iconIndex;

            return(embeddedIcon);
        }
        public static Icon ExtractIconFromFile(string fileAndParam, bool isLarge)
        {
            var hDummy  = new[] { IntPtr.Zero };
            var hIconEx = new[] { IntPtr.Zero };

            try {
                EmbeddedIconInfo embeddedIcon = GetEmbeddedIconInfo(fileAndParam);

                uint readIconCount = isLarge ? ExtractIconEx(embeddedIcon.FileName, 0, hIconEx, hDummy, 1) : ExtractIconEx(embeddedIcon.FileName, 0, hDummy, hIconEx, 1);

                if (readIconCount > 0 && hIconEx[0] != IntPtr.Zero)
                {
                    // Get first icon.
                    var extractedIcon = (Icon)Icon.FromHandle(hIconEx[0]).Clone();
                    return(extractedIcon);
                }
                else
                {
                    return(null);
                }
            } catch (Exception exc) {
                throw new ApplicationException("Could not extract icon", exc);
            } finally {
                foreach (IntPtr ptr in hIconEx)
                {
                    if (ptr != IntPtr.Zero)
                    {
                        DestroyIcon(ptr);
                    }
                }

                foreach (IntPtr ptr in hDummy)
                {
                    if (ptr != IntPtr.Zero)
                    {
                        DestroyIcon(ptr);
                    }
                }
            }
        }