代码之家  ›  专栏  ›  技术社区  ›  Philip Wallace

使用WebBrowser控件时,如何获取网站favicon?

  •  4
  • Philip Wallace  · 技术社区  · 16 年前

    我的应用程序中嵌入了一个Windows窗体WebBrowser控件。有没有任何方法可以使用WebBrowser或htmldocumentapi获得一个web页面favicon?即使从本地文件系统获得它也足够了。下载图标作为一个单独的操作将是最后的手段。。。

    4 回复  |  直到 15 年前
        1
  •  10
  •   Diogo Gomes    16 年前

    只需使用GET或类似的工具下载/favicon.ico文件(就像其他文件一样)。
    您还可以分析该页面以找到 可以 也是png。默认情况下,它是一个ICO文件。

    favicon文件的位置通常在 <link rel="shortcut icon" href="/favicon.ico" /> <head> 页面的节点。

    ,默认情况下,某些浏览器尝试下载/favicon.ico(即网站根文件夹中的favicon.ico文件) 没有 正在检查页面中的该元素。

    http://www.google.com/s2/favicons?domain=youtube.com (Try it)
    16x16 PNG图像 youtube的ICO favicon。

    http://www.google.com/s2/favicons?domain=stackoverflow.com (Try it)
    这将得到相同格式的stackoverflow favicon。

    可能看起来很棒但别忘了, 这个Google服务没有官方支持 他们可能会随时移除它。

        2
  •  2
  •   Sheng Jiang 蒋晟    16 年前

        3
  •  1
  •   Oded    16 年前

    这个 favicon 一个单独的文件。它不是HTML页面的一部分。

        4
  •  0
  •   Drew Chapin    11 年前

    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());
    }
    
    推荐文章