I have a Windows Forms WebBrowser control embedded in my application. Is there any way to get a web pages favicon using either the WebBrowser or HtmlDocument API? Even obtaining it from the local fi开发者_运维技巧le system would suffice. Downloading the icon as a separate operation would be a last resort...
Thanks.
Just download the /favicon.ico file using a GET or something similar (like you would for any other file).
You can also parse the page to find the favicon that might be also a png. By default it's an ICO file.
The location of favicon file is usually in <link rel="shortcut icon" href="/favicon.ico" />
in the <head>
node of the page.
Also, some browsers by default try to download /favicon.ico (that is, the favicon.ico file in the root folder of the website) without checking the page for that element.
Other idea would be using Google's S2:
http://www.google.com/s2/favicons?domain=youtube.com
(Try it)
This will get you a 16x16 PNG image of youtube's ICO favicon.
http://www.google.com/s2/favicons?domain=stackoverflow.com
(Try it)
This will get you the stackoverflow favicon in the same format.
It might seem awesome but don't forget, this Google service is not officially supported and they might remove it at anytime.
And the webbrowser control doesn't have an address bar, so it does not have application programming interface for address bar features like favicon.
The favicon
is a separate file. It is not part of the page HTML.
You will need to fetch it in a separate call.
I too was needing to do this, so I wrote this. Note that I'm using the native WebBrowser COM control instead of the .Net Wrapper, so if you're using the .Net Wrapper, some minor adjustments will need to be made.
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());
}
精彩评论