码迷,mamicode.com
首页 > 其他好文 > 详细

根据文件扩展名获取系统图标

时间:2015-05-10 09:41:21      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
  1        /// <summary>
  2        /// 根据文件后缀名获取系统图标。
  3        /// </summary>
  4        /// <param name="extension"></param>
  5        /// <returns></returns>
  6         public static ImageSource GetIconByExtension(string extension)
  7         {
  8             Icon smallIcon = null;
  9             Icon bigIcon = null;
 10             string describle = "";
 11             GetExtsIconAndDescription(extension, out bigIcon, out smallIcon, out describle);
 12             if (bigIcon != null)
 13             {
 14                 Bitmap bitmap = bigIcon.ToBitmap();
 15                 IntPtr hBitmap = bitmap.GetHbitmap();
 16                 ImageSource imageSource =
 17                      System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(
 18                           hBitmap, IntPtr.Zero, Int32Rect.Empty,
 19                           BitmapSizeOptions.FromEmptyOptions());
 20                 return imageSource;
 21             }
 22   /// <summary> 
 23         /// 通过扩展名得到图标和描述 
 24         /// </summary> 
 25         /// <param name="ext">扩展名</param> 
 26         /// <param name="LargeIcon">得到大图标</param> 
 27         /// <param name="smallIcon">得到小图标</param> 
 28         public static void GetExtsIconAndDescription(string ext, out Icon largeIcon, out Icon smallIcon, out string description)
 29         {
 30             largeIcon = smallIcon = null;
 31             description = "";
 32             var extsubkey = Registry.ClassesRoot.OpenSubKey(ext); //从注册表中读取扩展名相应的子键 
 33             if (extsubkey != null)
 34             {
 35                 var extdefaultvalue = (string)extsubkey.GetValue(null); //取出扩展名对应的文件类型名称 
 36 
 37                 //未取到值,返回预设图标 
 38                 if (extdefaultvalue == null)
 39                 {
 40                     GetDefaultIcon(out largeIcon, out smallIcon);
 41                     return;
 42                 }
 43 
 44                 var typesubkey = Registry.ClassesRoot.OpenSubKey(extdefaultvalue); //从注册表中读取文件类型名称的相应子键 
 45                 if (typesubkey != null)
 46                 {
 47                     description = (string)typesubkey.GetValue(null); //得到类型描述字符串 
 48                     var defaulticonsubkey = typesubkey.OpenSubKey("DefaultIcon"); //取默认图标子键 
 49                     if (defaulticonsubkey != null)
 50                     {
 51                         //得到图标来源字符串 
 52                         var defaulticon = (string)defaulticonsubkey.GetValue(null); //取出默认图标来源字符串 
 53                         var iconstringArray = defaulticon.Split(,);
 54                         int nIconIndex = 0;
 55                         if (iconstringArray.Length > 1) int.TryParse(iconstringArray[1], out nIconIndex);
 56                         //得到图标 
 57                         System.IntPtr phiconLarge = new IntPtr();
 58                         System.IntPtr phiconSmall = new IntPtr();
 59                         ExtractIconExW(iconstringArray[0].Trim("), nIconIndex, ref phiconLarge, ref phiconSmall, 1);
 60                         if (phiconLarge.ToInt32() > 0) largeIcon = Icon.FromHandle(phiconLarge);
 61                         if (phiconSmall.ToInt32() > 0) smallIcon = Icon.FromHandle(phiconSmall);
 62                     }
 63                 }
 64             }
 65         }
 66         /// <summary> 
 67         /// 获取缺省图标 
 68         /// </summary> 
 69         /// <param name="largeIcon"></param> 
 70         /// <param name="smallIcon"></param> 
 71         public static void GetDefaultIcon(out Icon largeIcon, out Icon smallIcon)
 72         {
 73             largeIcon = smallIcon = null;
 74             System.IntPtr phiconLarge = new IntPtr();
 75             System.IntPtr phiconSmall = new IntPtr();
 76             ExtractIconExW(Path.Combine(Environment.SystemDirectory, "shell32.dll"), 0, ref phiconLarge, ref phiconSmall, 1);
 77             if (phiconLarge.ToInt32() > 0) largeIcon = Icon.FromHandle(phiconLarge);
 78             if (phiconSmall.ToInt32() > 0) smallIcon = Icon.FromHandle(phiconSmall);
 79         }
 80         /// Return Type: UINT->unsigned int 
 81         ///lpszFile: LPCWSTR->WCHAR* 
 82         ///nIconIndex: int 
 83         ///phiconLarge: HICON* 
 84         ///phiconSmall: HICON* 
 85         ///nIcons: UINT->unsigned int 
 86         [DllImportAttribute("shell32.dll", EntryPoint = "ExtractIconExW", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]
 87         public static extern uint ExtractIconExW([System.Runtime.InteropServices.InAttribute()] [System.Runtime.InteropServices.MarshalAsAttribute(UnmanagedType.LPWStr)] string lpszFile, int nIconIndex, ref IntPtr phiconLarge, ref IntPtr phiconSmall, uint nIcons);
 88 
 89         public static void CreateFileIcon(string fileType, out Icon large, out Icon small)
 90         {
 91             string des;
 92 
 93             if (fileType.Trim() == "") //预设图标
 94             {
 95                 GetDefaultIcon(out large, out small);
 96             }
 97             else if (fileType.ToUpper() == ".EXE") //应用程序图标单独获取
 98             {
 99                 IntPtr l = IntPtr.Zero;
100                 IntPtr s = IntPtr.Zero;
101 
102                 ExtractIconExW(Path.Combine(Environment.SystemDirectory, "shell32.dll"), 2, ref l, ref s, 1);
103 
104                 large = Icon.FromHandle(l);
105                 small = Icon.FromHandle(s);
106             }
107             else //其它类型的图标
108             {
109                 GetExtsIconAndDescription(fileType, out large, out small, out des);
110             }
111 
112             if ((large == null) || (small == null)) //无法获取图标,预设图标
113                 GetDefaultIcon(out large, out small);
114         }
115         public static byte[] ImageToByte(Image image)
116         {
117             MemoryStream ms = new MemoryStream();
118             image.Save(ms, ImageFormat.Bmp);
119             byte[] bs = ms.ToArray();
120             ms.Close();
121             return bs;
122         }
123 
124         public static Image ByteToImage(byte[] bs)
125         {
126             MemoryStream ms = new MemoryStream(bs);
127             Bitmap bmp = new Bitmap(ms);
128             ms.Close();
129             return bmp;
130         }
131         [StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
132         public struct HICON__
133         {
134             public int unused;
135         }
136             return null;
137         }
View Code

 

根据文件扩展名获取系统图标

标签:

原文地址:http://www.cnblogs.com/zqhxl/p/4491757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!