|
4 | 4 | using System; |
5 | 5 | using System.Collections.Concurrent; |
6 | 6 | using System.Drawing; |
| 7 | +using System.Drawing.Imaging; |
7 | 8 | using System.IO; |
8 | 9 | using System.Linq; |
9 | 10 | using System.Net.Http; |
10 | 11 | using System.Security.Policy; |
11 | 12 | using System.Text; |
12 | 13 | using System.Threading.Tasks; |
13 | 14 | using System.Windows.Shapes; |
| 15 | +using static System.Windows.Forms.DataFormats; |
14 | 16 | using Path = System.IO.Path; |
15 | 17 |
|
16 | 18 | namespace SharpBrowser.Managers { |
| 19 | + |
17 | 20 | /// <summary> |
18 | 21 | /// Downloads and caches favicons for any given URL. |
19 | 22 | /// |
@@ -41,10 +44,22 @@ internal static class FavIconManager { |
41 | 44 | /// </summary> |
42 | 45 | private static readonly HttpClient httpClient = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) }; |
43 | 46 |
|
| 47 | + |
| 48 | + static byte[] NotFound_favico { get; set; } |
| 49 | + |
44 | 50 | public static void Init() { |
45 | 51 | Path.Combine(ConfigManager.AppDataPath, "FavIcons").EnsureFolderExists(); |
| 52 | + |
| 53 | + var favnf = new Bitmap(16, 16); |
| 54 | + var favg = Graphics.FromImage(favnf); |
| 55 | + |
| 56 | + favg.FillRectangle(Brushes.Beige,0,0,favnf.Width,favnf.Height); |
| 57 | + favg.DrawString("na", new Font( SystemFonts.DefaultFont.FontFamily,7f),Brushes.Black,0,0); |
| 58 | + |
| 59 | + NotFound_favico = favnf.ToByteArray(ImageFormat.Png) ; |
| 60 | + |
46 | 61 | } |
47 | | - |
| 62 | + |
48 | 63 | private static string GetIconPath(string domain) { |
49 | 64 | var cleanDomain = domain.RemovePrefix("www.").Replace(".", "_"); |
50 | 65 | return Path.Combine(ConfigManager.AppDataPath, "FavIcons\\" + cleanDomain + ".ico"); |
@@ -121,7 +136,12 @@ public static async void LoadFavicon(ChromiumWebBrowser browser, bool readCacheO |
121 | 136 | } |
122 | 137 | } |
123 | 138 |
|
| 139 | + |
124 | 140 | // NOT FOUND! |
| 141 | + //if you dont do this, there is residue favicon from previous Website. |
| 142 | + iconBitmap = NotFound_favico; |
| 143 | + OnLoaded(browser, iconBitmap); |
| 144 | + return; |
125 | 145 |
|
126 | 146 | //Console.WriteLine("No favicon could be retrieved."); |
127 | 147 | /*} |
@@ -215,5 +235,21 @@ private static void StoreFavicon(string domain, byte[] icon, bool saveToDisk) { |
215 | 235 | return null; |
216 | 236 | })() |
217 | 237 | "; |
| 238 | + |
| 239 | + } |
| 240 | + |
| 241 | + public static class ImageExtensions |
| 242 | + { |
| 243 | + public static byte[] ToByteArray(this Image image) => ToByteArray(image, ImageFormat.Png); |
| 244 | + |
| 245 | + public static byte[] ToByteArray(this Image image, ImageFormat format) |
| 246 | + { |
| 247 | + using (MemoryStream ms = new MemoryStream()) |
| 248 | + { |
| 249 | + image.Save(ms, format); |
| 250 | + return ms.ToArray(); |
| 251 | + } |
| 252 | + } |
218 | 253 | } |
| 254 | + |
219 | 255 | } |
0 commit comments