【问题标题】:How can I get a websites favicon when using the WebBrowser control?使用 WebBrowser 控件时如何获取网站图标?
【发布时间】:2010-04-07 14:43:01
【问题描述】:

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

谢谢。

【问题讨论】:

    标签: c# .net winforms webbrowser-control favicon


    【解决方案1】:

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

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

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

    其他想法是使用 Google 的 S2:

    http://www.google.com/s2/favicons?domain=youtube.com(Try it)
    这将为您提供 youtube 的 ICO favicon 的 16x16 PNG 图像

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

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

    【讨论】:

      【解决方案2】:

      而且 webbrowser 控件没有地址栏,因此它没有用于地址栏功能(如 favicon)的应用程序编程接口。

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

          我也需要这样做,所以我写了这个。请注意,我使用的是本机 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());
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2011-12-03
            • 1970-01-01
            • 2012-04-13
            • 1970-01-01
            相关资源
            最近更新 更多