How can I get website icons when using the WebBrowser control?

I have a Windows Forms WebBrowser control built into my application. Is there a way to get webpage icons using the WebBrowser API or HtmlDocument? It’s enough to even get it from the local file system. Downloading an icon as a separate operation will be the last resort ...

Thanks.

+4
source share
4 answers

Just load the /favicon.ico file using GET or something similar (as with any other file).
You can also analyze the page to find an icon, which could also be png. By default, this is an ICO file.

The location of the favicon file is usually located in the <link rel="shortcut icon" href="/favicon.ico" /> in the <head> node of the page.

In addition, some browsers by default try to load /favicon.ico (that is, the favicon.ico file in the website’s root folder) without checking the page for this element. p>

Another idea is to use Google S2:

http://www.google.com/s2/favicons?domain=youtube.com (Try it)
This will give you a 16x16 PNG image of the youtube ICU icon.

http://www.google.com/s2/favicons?domain=stackoverflow.com (Try it)
This will give you a stackoverflow background icon in the same format.

This may seem surprising, but do not forget, this Google service is not officially supported , and they can remove it at any time.

+9
source

And the webbrowser control does not have an address bar, so it does not have an application programming interface for address bar functions such as favicon.

+2
source

favicon is a separate file. This is not part of the HTML page.

You will need to get it in a separate call.

+1
source

I also needed to do this, so I wrote this. Please note that I use my own WebBrowser COM module, not .NET Wrapper, so if you use .Net Wrapper, you will need to make some minor adjustments.

 private void axWebBrowser1_DocumentComplete( object sender, AxSHDocVw.DWebBrowserEvents2_DocumentCompleteEvent e ) { try { Uri url = new Uri((string)e.uRL); string favicon = null; mshtml.HTMLDocument document = axWebBrowser1.Document as mshtml.HTMLDocument; if( document != null ) { mshtml.IHTMLElementCollection linkTags = document.getElementsByTagName("link"); foreach( object obj in linkTags ) { mshtml.HTMLLinkElement link = obj as mshtml.HTMLLinkElement; if( link != null ) { if( !String.IsNullOrEmpty(link.rel) && !String.IsNullOrEmpty(link.href) && link.rel.Equals("shortcut icon",StringComparison.CurrentCultureIgnoreCase) ) { //TODO: Bug - Can't handle relative favicon URL's favicon = link.href; } } } } if( String.IsNullOrEmpty(favicon) && !String.IsNullOrEmpty(url.Host) ) { if( url.IsDefaultPort ) favicon = String.Format("{0}://{1}/favicon.ico",url.Scheme,url.Host); else favicon = String.Format("{0}://{1}:{2}/favicon.ico",url.Scheme,url.Host,url.Port); } if( !String.IsNullOrEmpty(favicon) ) { WebRequest request = WebRequest.Create(favicon); request.BeginGetRequestStream(new AsyncCallback(SetFavicon), request); } } catch { this.Icon = null; } } private void SetFavicon( IAsyncResult result ) { WebRequest request = (WebRequest)result.AsyncState; WebResponse response = request.GetResponse(); Bitmap bitmap = new Bitmap(Image.FromStream(response.GetResponseStream())); this.Icon = Icon.FromHandle(bitmap.GetHicon()); } 
0
source

Source: https://habr.com/ru/post/1306253/


All Articles