跳到主要内容

github/tai

公开

Tai · Windows 软件与网站使用时长统计。上游 Planshit/Tai 的源码镜像,非灵栈原创;MIT 许可。来源:https://github.com/Planshit/Tai 。本次为手动同步,暂不提供安装包。

浏览文件

Iconer.cs

200 行 · 7.3 KB历史下载
1using System;
2using System.Collections.Generic;
3using System.Diagnostics;
4using System.Drawing;
5using System.IO;
6using System.Linq;
7using System.Text;
8using System.Text.RegularExpressions;
9using System.Threading.Tasks;
10using System.Windows;
11using System.Windows.Interop;
12using System.Windows.Media;
13using System.Windows.Media.Imaging;
14using System.Xml;
15using System.Xml.Serialization;
16
17namespace Core.Librarys
18{
19 public class Iconer
20 {
21 /// <summary>
22 /// 格式化图标文件名称
23 /// </summary>
24 /// <param name="processname">进程名称</param>
25 /// <param name="desc">进程简介</param>
26 /// <returns>返回正确的文件名称</returns>
27 private static string FromatIconFileName(string processname, string desc)
28 {
29 string iconName = (processname + desc).Replace(" ", "") + ".png";
30
31 // 清除无效字符
32 iconName = iconName.Replace("/", "");
33 iconName = iconName.Replace("\\", "");
34 iconName = iconName.Replace(":", "");
35 iconName = iconName.Replace("*", "");
36 iconName = iconName.Replace("?", "");
37 iconName = iconName.Replace("\"", "");
38 iconName = iconName.Replace("'", "");
39 iconName = iconName.Replace("<", "");
40 iconName = iconName.Replace(">", "");
41 iconName = iconName.Replace("|", "");
42
43 return iconName;
44 }
45 public static string Get(string processname, string desc, bool isRelativePath = true)
46 {
47 string iconName = FromatIconFileName(processname, desc);
48 string iconPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
49 "AppIcons", iconName);
50 if (!File.Exists(iconPath))
51 {
52 return "pack://application:,,,/Tai;component/Resources/Icons/defaultIcon.png";
53 }
54
55 if (isRelativePath)
56 {
57 return Path.Combine(
58 "AppIcons", iconName);
59 }
60 return iconPath;
61 }
62 /// <summary>
63 /// 提取icon为Png格式并保存到程序目录下
64 /// </summary>
65 /// <param name="file"></param>
66 /// <param name="processname"></param>
67 /// <param name="desc"></param>
68 /// <param name="isRelativePath">是否返回相对路径</param>
69 /// <returns>返回提取到程序目录下的路径</returns>
70 public static string ExtractFromFile(string file, string processname, string desc, bool isCheck = true, bool isRelativePath = true)
71 {
72 try
73 {
74 string iconName = FromatIconFileName(processname, desc);
75
76 string iconPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory,
77 "AppIcons", iconName);
78
79 string relativePath = Path.Combine(
80 "AppIcons", iconName);
81
82 if (isCheck && File.Exists(iconPath))
83 {
84 if (isRelativePath)
85 {
86 return relativePath;
87 }
88 return iconPath;
89 }
90 string dir = Path.GetDirectoryName(iconPath);
91 if (!Directory.Exists(dir))
92 {
93 Directory.CreateDirectory(dir);
94 }
95
96 // uwp app icon handle
97
98 if (file.IndexOf("WindowsApps") != -1)
99 {
100 // 只有在包含此关键字时才去处理
101
102 // 继续判断是否是uwp程序
103 string appdir = file.Substring(0, file.Length - file.Split('\\').Last().Length);
104 string appxManifestPath = appdir + "AppxManifest.xml";
105 if (File.Exists(appxManifestPath))
106 {
107 // 是uwp程序
108 Debug.WriteLine("is uwp!" + appxManifestPath);
109 // 读取描述文件
110 string manifestText = File.ReadAllText(appxManifestPath);
111 var match = Regex.Match(manifestText, @"<Logo>(.*?)</Logo>");
112 string logoName = match.Groups[1].Value;
113
114 string iconFile = string.Empty;
115
116 string logo100 = logoName.Replace(".png", ".scale-100.png");
117 string logo125 = logoName.Replace(".png", ".scale-125.png");
118 string logo150 = logoName.Replace(".png", ".scale-150.png");
119 string logo200 = logoName.Replace(".png", ".scale-200.png");
120 string logo400 = logoName.Replace(".png", ".scale-400.png");
121
122 if (File.Exists(appdir + logo100))
123 {
124 iconFile = appdir + logo100;
125 }
126 else if (File.Exists(appdir + logo125))
127 {
128 iconFile = appdir + logo125;
129 }
130 else if (File.Exists(appdir + logo150))
131 {
132 iconFile = appdir + logo150;
133 }
134 else if (File.Exists(appdir + logo200))
135 {
136 iconFile = appdir + logo200;
137 }
138 else if (File.Exists(appdir + logo400))
139 {
140 iconFile = appdir + logo400;
141 }
142 else
143 {
144 return string.Empty;
145 }
146
147 if (!string.IsNullOrEmpty(iconFile) && File.Exists(iconFile))
148 {
149 // copy to tai dir
150
151 File.Copy(iconFile, iconPath);
152
153 if (isRelativePath)
154 {
155 return relativePath;
156 }
157
158 return iconPath;
159 }
160 return string.Empty;
161 }
162 }
163
164
165
166 // exe app icon handle
167
168 Icon ico = Icon.ExtractAssociatedIcon(file);
169
170 using (var fileStream = new FileStream(iconPath, FileMode.Create))
171 {
172 BitmapEncoder encoder = new PngBitmapEncoder();
173 encoder.Frames.Add(BitmapFrame.Create(ToImageSource(ico) as BitmapSource));
174 encoder.Save(fileStream);
175 }
176
177 if (isRelativePath)
178 {
179 return relativePath;
180 }
181
182 return iconPath;
183 }
184 catch (Exception ex)
185 {
186 Logger.Error(ex.Message + ",File: " + file + ",Process: " + processname);
187 return string.Empty;
188 }
189 }
190 public static ImageSource ToImageSource(Icon icon)
191 {
192 ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon(
193 icon.Handle,
194 Int32Rect.Empty,
195 BitmapSizeOptions.FromEmptyOptions());
196
197 return imageSource;
198 }
199 }
200}