| 1 | using System; |
| 2 | using System.Collections.Generic; |
| 3 | using System.Diagnostics; |
| 4 | using System.Drawing; |
| 5 | using System.IO; |
| 6 | using System.Linq; |
| 7 | using System.Text; |
| 8 | using System.Text.RegularExpressions; |
| 9 | using System.Threading.Tasks; |
| 10 | using System.Windows; |
| 11 | using System.Windows.Interop; |
| 12 | using System.Windows.Media; |
| 13 | using System.Windows.Media.Imaging; |
| 14 | using System.Xml; |
| 15 | using System.Xml.Serialization; |
| 16 | |
| 17 | namespace 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 | /// |
| 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 | |
| 97 | |
| 98 | if (file.IndexOf("WindowsApps") != -1) |
| 99 | { |
| 100 | |
| 101 | |
| 102 | |
| 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 | |
| 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 | |
| 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 | |
| 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 | } |