使用 WebBrowser 控件时如何获取网站图标?

发布于 2024-08-28 17:12:03 字数 143 浏览 9 评论 0 原文

我的应用程序中嵌入了一个 Windows 窗体 WebBrowser 控件。有没有办法使用 WebBrowser 或 HtmlDocument API 获取网页图标?甚至从本地文件系统获取它就足够了。下载图标作为单独的操作将是最后的手段......

谢谢。

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 file system would suffice. Downloading the icon as a separate operation would be a last resort...

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

挥剑断情 2024-09-04 17:12:03

只需使用 GET 或类似的方式下载 /favicon.ico 文件(就像下载任何其他文件一样)。
您还可以解析页面以查找可能也是 png 的图标。默认情况下它是一个 ICO 文件。

favicon文件的位置通常在节点中的页面。

此外,某些浏览器默认会尝试下载 /favicon.ico(即网站根文件夹中的 favicon.ico 文件)而不检查该页面元素。

其他想法是使用 Google 的 S2:

http://www.google.com/s2/favicons?domain=youtube.com (尝试一下)
这将为您提供 YouTube ICO 图标的 16x16 PNG 图像

http://www.google.com/s2/favicons?domain=stackoverflow.com (尝试一下)
这将为您提供相同格式的 stackoverflow 图标。

它可能看起来很棒,但请不要忘记,此 Google 服务并未得到官方支持,他们可能随时将其删除。

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.

半仙 2024-09-04 17:12:03

而且网络浏览器控件没有地址栏,因此它没有用于地址栏功能(如网站图标)的应用程序编程接口。

And the webbrowser control doesn't have an address bar, so it does not have application programming interface for address bar features like favicon.

墨小墨 2024-09-04 17:12:03

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

您需要在单独的调用中获取它。

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

You will need to fetch it in a separate call.

过气美图社 2024-09-04 17:12:03

我也需要这样做,所以我写了这篇文章。请注意,我使用的是本机 WebBrowser COM 控件而不是 .Net Wrapper,因此如果您使用 .Net Wrapper,则需要进行一些细微的调整。

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());
}

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());
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文