/// <summary> /// Extract the icon from file, and return icon information /// </summary> /// <param name="path">File path, /// such as ex: "C:\\Program Files\\NetMeeting\\conf.exe,1".</param> /// <param name="size">The desired icon size</param> /// <param name="linkOverlay">Whether to include the link icon</param> /// <param name="shfi">The icon size</param> /// <returns>This method always returns an icon with the especified size and thier information.</returns> public static Icon ExtractIconFromFileEx(string path, IconSize size, bool linkOverlay, ref Shell32.SHFILEINFO shfi) { EmbeddedIconInfo embeddedIcon = GetEmbeddedIconInfo(Environment.ExpandEnvironmentVariables(path)); //Gets the handle of the icon. return(GetFileIcon(embeddedIcon.FileName, size, linkOverlay, ref shfi)); }
/// <summary> /// Extract the icon from file. /// </summary> /// <param name="path">File path, /// 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 path) { EmbeddedIconInfo embeddedIcon = GetEmbeddedIconInfo(Environment.ExpandEnvironmentVariables(path)); //Gets the handle of the icon. return(ExtractIconFromFile(embeddedIcon.FileName, embeddedIcon.IconIndex)); }
protected static EmbeddedIconInfo GetEmbeddedIconInfo(string fileAndParam) { EmbeddedIconInfo embeddedIcon = new EmbeddedIconInfo("", 0); if (string.IsNullOrEmpty(fileAndParam)) { return(embeddedIcon); } string fileName = String.Empty; int iconIndex = 0; string iconIndexString = String.Empty; int commaIndex = fileAndParam.IndexOf(","); if (commaIndex > 0) { fileName = fileAndParam.Substring(0, commaIndex); iconIndexString = fileAndParam.Substring(commaIndex + 1); } else { fileName = fileAndParam; } if (!string.IsNullOrEmpty(iconIndexString)) { iconIndex = int.Parse(iconIndexString); if (iconIndex < 0) { iconIndex = 0; } } embeddedIcon.FileName = fileName; embeddedIcon.IconIndex = iconIndex; return(embeddedIcon); }
/// <summary> /// دریافت آیکن یک فایل /// </summary> /// <param name="fileAndParam">آدرس فایل و ایندکس آیکن</param> /// <param name="isLarge">آیا یک آیک بزرگ است یا خیر</param> /// <returns></returns> public static Icon ExtractIconFromFile(EmbeddedIconInfo fileAndParam, bool isLarge) { unsafe { uint readIconCount = 0; IntPtr[] hDummy = new IntPtr[1] { IntPtr.Zero }; IntPtr[] hIconEx = new IntPtr[1] { IntPtr.Zero }; try { if (isLarge) { readIconCount = ExtractIconEx(fileAndParam.FileName, 0, hIconEx, hDummy, 1); } else { readIconCount = ExtractIconEx(fileAndParam.FileName, 0, hDummy, hIconEx, 1); } if (readIconCount > 0 && hIconEx[0] != IntPtr.Zero) { Icon 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); } } } } }
/// <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> public static EmbeddedIconInfo GetEmbeddedIconInfo(string fileAndParam) { EmbeddedIconInfo 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. try { iconIndex = int.Parse(iconIndexString); if (iconIndex < 0) { iconIndex = 0; //To avoid the invalid index. } } catch //(Exception ex) { //MessageBox.Show("Source: " + ex.Source + Environment.NewLine + ex.Message); } } embeddedIcon.FileName = fileName; embeddedIcon.IconIndex = iconIndex; return(embeddedIcon); }
public static Icon ExtractIconFromFile(string fileAndParam) { try { EmbeddedIconInfo embeddedIconInfo = getEmbeddedIconInfo(fileAndParam); return(Icon.FromHandle(ExtractIcon(0, embeddedIconInfo.FileName, embeddedIconInfo.IconIndex))); } catch (Exception ex) { throw ex; } }
/// <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> public 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 this: //"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. * may cause unexpeced benaviour */ } embeddedIcon.FileName = fileName; embeddedIcon.IconIndex = iconIndex; return(embeddedIcon); }
/// <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; } }
/// <summary> /// Extract the icon from file. /// </summary> /// <param name="path">File path, /// 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 path, bool isLarge) { IntPtr[] hDummy = new IntPtr[] { IntPtr.Zero }; IntPtr[] hIconEx = new IntPtr[] { IntPtr.Zero }; try { EmbeddedIconInfo embeddedIcon = GetEmbeddedIconInfo(Environment.ExpandEnvironmentVariables(path)); uint readIconCount = isLarge ? Shell32.ExtractIconEx(embeddedIcon.FileName, 0, hIconEx, hDummy, 1) : Shell32.ExtractIconEx(embeddedIcon.FileName, 0, hDummy, hIconEx, 1); if (readIconCount > 0 && hIconEx[0] != IntPtr.Zero) { // Get first icon. return(GetManagedIcon(hIconEx[0])); } 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) { User32.DestroyIcon(ptr); } } foreach (IntPtr ptr in hDummy) { if (ptr != IntPtr.Zero) { User32.DestroyIcon(ptr); } } } }
public static Icon ExtractIconFromFile(string fileAndParam, bool isLarge) { IntPtr[] numArray1 = new IntPtr[1] { IntPtr.Zero }; IntPtr[] numArray2 = new IntPtr[1] { IntPtr.Zero }; try { EmbeddedIconInfo embeddedIconInfo = getEmbeddedIconInfo(fileAndParam); return((!isLarge ? ExtractIconEx(embeddedIconInfo.FileName, 0, numArray1, numArray2, 1U) : ExtractIconEx(embeddedIconInfo.FileName, 0, numArray2, numArray1, 1U)) > 0U && numArray2[0] != IntPtr.Zero ? (Icon)Icon.FromHandle(numArray2[0]).Clone() : (Icon)null); } catch (Exception ex) { throw new ApplicationException("Could not extract icon", ex); } finally { foreach (IntPtr hIcon in numArray2) { if (hIcon != IntPtr.Zero) { DestroyIcon(hIcon); } } foreach (IntPtr hIcon in numArray1) { if (hIcon != IntPtr.Zero) { DestroyIcon(hIcon); } } } }
/// <summary> /// دریافت آیکن یک پسوند /// </summary> /// <param name="extension">پسوند</param> /// <returns></returns> public static EmbeddedIconInfo GetExtensionDefaultIcon(string extension) { if (string.IsNullOrEmpty(extension)) { throw new ArgumentNullException(extension); } EmbeddedIconInfo _EmbeddedIconInfo = null; RegistryKey classesRoot = Registry.ClassesRoot; if (!extension.StartsWith(".")) { extension = "." + extension; } RegistryKey rkFileType = classesRoot.OpenSubKey(extension); if (rkFileType == null) { return(null); } object defaultValue = rkFileType.GetValue(""); if (defaultValue == null) { return(null); } string defaultIcon = defaultValue.ToString() + "\\DefaultIcon"; RegistryKey rkFileIcon = classesRoot.OpenSubKey(defaultIcon); if (rkFileIcon != null) { object value = rkFileIcon.GetValue(""); if (value != null) { string fileParam = value.ToString().Replace("\"", ""); _EmbeddedIconInfo = GetEmbeddedIconInfo(fileParam); } rkFileIcon.Close(); } rkFileType.Close(); classesRoot.Close(); return(_EmbeddedIconInfo); }
protected static EmbeddedIconInfo getEmbeddedIconInfo( string fileAndParam) { EmbeddedIconInfo embeddedIconInfo = new EmbeddedIconInfo(); if (string.IsNullOrEmpty(fileAndParam)) { return(embeddedIconInfo); } string empty = string.Empty; int num = 0; string s = string.Empty; int length = fileAndParam.IndexOf(","); string str; if (length > 0) { str = fileAndParam.Substring(0, length); s = fileAndParam.Substring(length + 1); } else { str = fileAndParam; } if (!string.IsNullOrEmpty(s)) { num = int.Parse(s); if (num < 0) { num = 0; } } embeddedIconInfo.FileName = str; embeddedIconInfo.IconIndex = num; return(embeddedIconInfo); }
/// <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 System.Drawing.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, embeddedIcon.IconIndex, hIconEx, hDummy, 1); } else { readIconCount = ExtractIconEx (embeddedIcon.FileName, embeddedIcon.IconIndex, hDummy, hIconEx, 1); } if (readIconCount > 0 && hIconEx[0] != IntPtr.Zero) { //Get first icon. System.Drawing.Icon extractedIcon = (System.Drawing.Icon)System.Drawing.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); } } } } }
public static Icon ExtractIconFromFile(EmbeddedIconInfo fileAndParam) { IntPtr lIcon = ExtractIcon(0, fileAndParam.FileName, fileAndParam.IconIndex); return(Icon.FromHandle(lIcon)); }
/// <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> public static EmbeddedIconInfo GetEmbeddedIconInfo(string fileAndParam) { EmbeddedIconInfo 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. try { iconIndex = int.Parse(iconIndexString); if (iconIndex < 0) iconIndex = 0; //To avoid the invalid index. } catch (Exception ex) { //MessageBox.Show("Source: " + ex.Source + Environment.NewLine + ex.Message); } } embeddedIcon.FileName = fileName; embeddedIcon.IconIndex = iconIndex; return embeddedIcon; }
/// <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) { var 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; }
/// <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> public 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 this: //"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. * may cause unexpeced benaviour */ } embeddedIcon.FileName = fileName; embeddedIcon.IconIndex = iconIndex; return embeddedIcon; }
/// <summary> Извлекает иконку из файла. /// </summary> /// <param name="fileAndParam"> /// Строка параметров, /// например: "C:\\Program Files\\NetMeeting\\conf.exe,1".</param> /// <param name="isLarge"> /// Возвращается икона большая (может быть 32x32 /// или маленькая иконка (16х16 пикселей).</param> public static Bitmap GetIcon(string fileAndParam, bool isLarge) { EmbeddedIconInfo embeddedIcon = GetEmbeddedIconInfo(fileAndParam); return(GetIcon(embeddedIcon.FileName, embeddedIcon.IconIndex, isLarge)); }