#region using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Runtime.InteropServices; using Microsoft.Win32; #endregion namespace CSFrameworkV5.Common { /// /// 图标工具 /// public class IconTool { /// /// 数组转换为图像 /// /// /// public static Image ByteToImage(byte[] bs) { var ms = new MemoryStream(bs); var bmp = new Bitmap(ms); ms.Close(); return bmp; } /// /// 获取文件的ICO图标 /// /// 文件类型(*.exe,*.dll) /// 大图标 /// 返回小图标 public static void CreateFileIcon(string fileType, out Icon large, out Icon small) { string des; if (fileType.Trim() == "") //预设图标 { GetDefaultIcon(out large, out small); } else if (fileType.ToUpper() == ".EXE") //应用程序图标单独获取 { var l = IntPtr.Zero; var s = IntPtr.Zero; ExtractIconExW( Path.Combine(Environment.SystemDirectory, "shell32.dll"), 2, ref l, ref s, 1); large = Icon.FromHandle(l); small = Icon.FromHandle(s); } else //其它类型的图标 { GetExtsIconAndDescription(fileType, out large, out small, out des); } if (large == null || small == null) //无法获取图标,预设图标 GetDefaultIcon(out large, out small); } /// Return Type: UINT->unsigned int /// lpszFile: LPCWSTR->WCHAR* /// nIconIndex: int /// phiconLarge: HICON* /// phiconSmall: HICON* /// nIcons: UINT->unsigned int [DllImportAttribute("shell32.dll", EntryPoint = "ExtractIconExW", CallingConvention = CallingConvention.StdCall)] public static extern uint ExtractIconExW( [InAttribute] [MarshalAsAttribute(UnmanagedType.LPWStr)] string lpszFile, int nIconIndex, ref IntPtr phiconLarge, ref IntPtr phiconSmall, uint nIcons); /// /// 获取缺省图标 /// /// /// public static void GetDefaultIcon(out Icon largeIcon, out Icon smallIcon) { largeIcon = smallIcon = null; var phiconLarge = new IntPtr(); var phiconSmall = new IntPtr(); ExtractIconExW( Path.Combine(Environment.SystemDirectory, "shell32.dll"), 0, ref phiconLarge, ref phiconSmall, 1); if (phiconLarge.ToInt32() > 0) largeIcon = Icon.FromHandle(phiconLarge); if (phiconSmall.ToInt32() > 0) smallIcon = Icon.FromHandle(phiconSmall); } /// /// 通过扩展名得到图标和描述 /// /// 扩展名 /// 得到大图标 /// 得到小图标 public static void GetExtsIconAndDescription(string ext, out Icon largeIcon, out Icon smallIcon, out string description) { //默认值 largeIcon = null; smallIcon = null; description = ""; if (string.IsNullOrWhiteSpace(ext)) return; if (ext.Length > 4) return; //控制文件扩展名长度4位,//防止注册表操纵 .jpg/.pdf var extsubkey = Registry.ClassesRoot.OpenSubKey(ext); //从注册表中读取扩展名相应的子键 if (extsubkey != null) { var extdefaultvalue = extsubkey.GetValue(null).ToStringEx(); //取出扩展名对应的文件类型名称 var typesubkey = Registry.ClassesRoot .OpenSubKey(extdefaultvalue); //从注册表中读取文件类型名称的相应子键 if (typesubkey != null) { description = typesubkey.GetValue(null).ToStringEx(); //得到类型描述字符串 var defaulticonsubkey = typesubkey.OpenSubKey("DefaultIcon"); //取默认图标子键 if (defaulticonsubkey != null) { //得到图标来源字符串 var defaulticon = defaulticonsubkey.GetValue(null) .ToStringEx(); //取出默认图标来源字符串 var iconstringArray = defaulticon.Split(','); var nIconIndex = 0; if (iconstringArray.Length > 1) int.TryParse(iconstringArray[1], out nIconIndex); //得到图标 var phiconLarge = new IntPtr(); var phiconSmall = new IntPtr(); ExtractIconExW(iconstringArray[0].Trim('"'), nIconIndex, ref phiconLarge, ref phiconSmall, 1); if (phiconLarge.ToInt32() > 0) largeIcon = Icon.FromHandle(phiconLarge); if (phiconSmall.ToInt32() > 0) smallIcon = Icon.FromHandle(phiconSmall); } } } } /// /// 图像转换为数组 /// /// /// public static byte[] ImageToByte(Image image) { var ms = new MemoryStream(); image.Save(ms, ImageFormat.Bmp); var bs = ms.ToArray(); ms.Close(); return bs; } } /// /// 图标句柄 /// [StructLayoutAttribute(LayoutKind.Sequential)] public struct HICON__ { public int unused; } }