Ejemplo n.º 1
0
        /// <summary>
        /// Extract the icon from file.
        /// </summary>
        /// <param name="fileAndParam">The params string,
        /// such as ex: "C:\\Program Files\\NetMeeting\\conf.exe,1".</param>
        /// <returns>This method always returns the large size of the icon (may be 32x32 px).</returns>
        public static Icon ExtractIconFromFile(string fileAndParam)
        {
            try
            {
                EmbeddedIconInfo embeddedIcon = getEmbeddedIconInfo(fileAndParam);

                //Gets the handle of the icon.
                IntPtr lIcon = ExtractIcon(0, embeddedIcon.FileName, embeddedIcon.IconIndex);

                //Gets the real icon.
                return(Icon.FromHandle(lIcon));
            }
            catch (Exception exc)
            {
                throw exc;
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Parses the parameters string to the structure of EmbeddedIconInfo.
        /// </summary>
        /// <param name="fileAndParam">The params string,
        /// such as ex: "C:\\Program Files\\NetMeeting\\conf.exe,1".</param>
        /// <returns></returns>
        protected static EmbeddedIconInfo getEmbeddedIconInfo(string fileAndParam)
        {
            EmbeddedIconInfo embeddedIcon = new EmbeddedIconInfo();

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

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

            //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);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Parses the parameters string to the structure of EmbeddedIconInfo.
        /// </summary>
        /// <param name="fileAndParam">The params string, 
        /// such as ex: "C:\\Program Files\\NetMeeting\\conf.exe,1".</param>
        /// <returns></returns>
        protected static EmbeddedIconInfo getEmbeddedIconInfo(string fileAndParam)
        {
            EmbeddedIconInfo embeddedIcon = new EmbeddedIconInfo();

            if (string.IsNullOrEmpty(fileAndParam))
                return embeddedIcon;

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

            //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;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Extract the icon from file.
        /// </summary>
        /// <param name="fileAndParam">The params string,
        /// such as ex: "C:\\Program Files\\NetMeeting\\conf.exe,1".</param>
        /// <param name="isLarge">
        /// Determines the returned icon is a large (may be 32x32 px)
        /// or small icon (16x16 px).</param>
        public static Icon ExtractIconFromFile(string fileAndParam, bool isLarge)
        {
            unsafe
            {
                uint     readIconCount = 0;
                IntPtr[] hDummy        = new IntPtr[1] {
                    IntPtr.Zero
                };
                IntPtr[] hIconEx = new IntPtr[1] {
                    IntPtr.Zero
                };

                try
                {
                    EmbeddedIconInfo embeddedIcon = getEmbeddedIconInfo(fileAndParam);

                    if (isLarge)
                    {
                        readIconCount = ExtractIconEx(embeddedIcon.FileName, 0, hIconEx, hDummy, 1);
                    }
                    else
                    {
                        readIconCount = ExtractIconEx(embeddedIcon.FileName, 0, hDummy, hIconEx, 1);
                    }

                    if (readIconCount > 0 && hIconEx[0] != IntPtr.Zero)
                    {
                        // Get first icon.
                        Icon extractedIcon = (Icon)Icon.FromHandle(hIconEx[0]).Clone();

                        return(extractedIcon);
                    }
                    else // No icon read
                    {
                        return(null);
                    }
                }
                catch (Exception exc)
                {
                    // Extract icon error.
                    throw new ApplicationException("Could not extract icon", exc);
                }
                finally
                {
                    // Release resources.
                    foreach (IntPtr ptr in hIconEx)
                    {
                        if (ptr != IntPtr.Zero)
                        {
                            DestroyIcon(ptr);
                        }
                    }

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