Skip to content

Commit 6bdd422

Browse files
fix favicon -- if site has no fav return dummy Fav ... so it deletes previous websites residue fav.
1 parent 2bf1950 commit 6bdd422

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

Diff for: src/Managers/FavIconManager.cs

+37-1
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44
using System;
55
using System.Collections.Concurrent;
66
using System.Drawing;
7+
using System.Drawing.Imaging;
78
using System.IO;
89
using System.Linq;
910
using System.Net.Http;
1011
using System.Security.Policy;
1112
using System.Text;
1213
using System.Threading.Tasks;
1314
using System.Windows.Shapes;
15+
using static System.Windows.Forms.DataFormats;
1416
using Path = System.IO.Path;
1517

1618
namespace SharpBrowser.Managers {
19+
1720
/// <summary>
1821
/// Downloads and caches favicons for any given URL.
1922
///
@@ -41,10 +44,22 @@ internal static class FavIconManager {
4144
/// </summary>
4245
private static readonly HttpClient httpClient = new HttpClient() { Timeout = TimeSpan.FromSeconds(5) };
4346

47+
48+
static byte[] NotFound_favico { get; set; }
49+
4450
public static void Init() {
4551
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+
4661
}
47-
62+
4863
private static string GetIconPath(string domain) {
4964
var cleanDomain = domain.RemovePrefix("www.").Replace(".", "_");
5065
return Path.Combine(ConfigManager.AppDataPath, "FavIcons\\" + cleanDomain + ".ico");
@@ -121,7 +136,12 @@ public static async void LoadFavicon(ChromiumWebBrowser browser, bool readCacheO
121136
}
122137
}
123138

139+
124140
// 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;
125145

126146
//Console.WriteLine("No favicon could be retrieved.");
127147
/*}
@@ -215,5 +235,21 @@ private static void StoreFavicon(string domain, byte[] icon, bool saveToDisk) {
215235
return null;
216236
})()
217237
";
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+
}
218253
}
254+
219255
}

0 commit comments

Comments
 (0)