啊鑫
2024-07-09 0552fcc8cb73fc3021e2915129f55a42ed3f20e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#region
 
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32;
 
#endregion
 
namespace CSFrameworkV5.Common
{
    /// <summary>
    ///     图标工具
    /// </summary>
    public class IconTool
    {
        /// <summary>
        ///     数组转换为图像
        /// </summary>
        /// <param name="bs"></param>
        /// <returns></returns>
        public static Image ByteToImage(byte[] bs)
        {
            var ms = new MemoryStream(bs);
            var bmp = new Bitmap(ms);
            ms.Close();
            return bmp;
        }
 
        /// <summary>
        ///     获取文件的ICO图标
        /// </summary>
        /// <param name="fileType">文件类型(*.exe,*.dll)</param>
        /// <param name="large">大图标</param>
        /// <param name="small">返回小图标</param>
        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);
 
        /// <summary>
        ///     获取缺省图标
        /// </summary>
        /// <param name="largeIcon"></param>
        /// <param name="smallIcon"></param>
        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);
        }
 
        /// <summary>
        ///     通过扩展名得到图标和描述
        /// </summary>
        /// <param name="ext">扩展名</param>
        /// <param name="LargeIcon">得到大图标</param>
        /// <param name="smallIcon">得到小图标</param>
        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);
                    }
                }
            }
        }
 
        /// <summary>
        ///     图像转换为数组
        /// </summary>
        /// <param name="image"></param>
        /// <returns></returns>
        public static byte[] ImageToByte(Image image)
        {
            var ms = new MemoryStream();
            image.Save(ms, ImageFormat.Bmp);
            var bs = ms.ToArray();
            ms.Close();
            return bs;
        }
    }
 
    /// <summary>
    ///     图标句柄
    /// </summary>
    [StructLayoutAttribute(LayoutKind.Sequential)]
    public struct HICON__
    {
        public int unused;
    }
}