日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

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

發(fā)布時間:2024/7/19 windows 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 根据文件扩展名获取系统图标 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1 /// <summary> 2 /// 根據(jù)文件后綴名獲取系統(tǒng)圖標。 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); //從注冊表中讀取擴展名相應(yīng)的子鍵 33 if (extsubkey != null) 34 { 35 var extdefaultvalue = (string)extsubkey.GetValue(null); //取出擴展名對應(yīng)的文件類型名稱 36 37 //未取到值,返回預(yù)設(shè)圖標 38 if (extdefaultvalue == null) 39 { 40 GetDefaultIcon(out largeIcon, out smallIcon); 41 return; 42 } 43 44 var typesubkey = Registry.ClassesRoot.OpenSubKey(extdefaultvalue); //從注冊表中讀取文件類型名稱的相應(yīng)子鍵 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() == "") //預(yù)設(shè)圖標 94 { 95 GetDefaultIcon(out large, out small); 96 } 97 else if (fileType.ToUpper() == ".EXE") //應(yīng)用程序圖標單獨獲取 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)) //無法獲取圖標,預(yù)設(shè)圖標 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

?

轉(zhuǎn)載于:https://www.cnblogs.com/zqhxl/p/4491757.html

總結(jié)

以上是生活随笔為你收集整理的根据文件扩展名获取系统图标的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。