将HTML网页中的图标解压缩到文件(png或ico)中,以加载为TriView图标

时间:2014-02-14 16:28:56

标签: c# wpf url treeview ico

下面的代码提取一个Png或(ico文件已注释掉),当Paint显示时,会显示一个小的16x16图标,但是这个图标文件(png或ico不能用作Treeview图标。其他更大的Png /然而,Ico文件可以正常工作。

     public static bool GetURLIconFile(string webpageUrl, string IconFile)
    {
        //returns the icon for webpageURL in IconFile
        string siteUrl = GetWebSite(webpageUrl);  // just returns URL of site
        var url = GetURLIcon("http://" + siteUrl);
        if (url == null)
        {
            DeleteFile(IconFile);
            return false;
        }
       try
       {
            HttpWebRequest w = (HttpWebRequest)HttpWebRequest.Create(url);

            w.AllowAutoRedirect = true;

            HttpWebResponse r = (HttpWebResponse)w.GetResponse();

            System.Drawing.Image ico;
            using (Stream s = r.GetResponseStream())
            {
                ico = System.Drawing.Image.FromStream(s);
                ico.Save(IconFile, System.Drawing.Imaging.ImageFormat.Png); // forpng
                // ico.Save(IconFile, System.Drawing.Imaging.ImageFormat.ico); // forico


            }
            return true;
        }
        catch
        {
            DeleteFile(IconFile);
            return false;
        } 
    }
    public static Uri GetURLIcon(string siteUrl)
    {

        // try looking for a /favicon.ico first           
        try
        {
            var url = new Uri(siteUrl);
            var faviconUrl = new Uri(string.Format("{0}://{1}/favicon.ico", url.Scheme, url.Host));
            try
            {
                using (var httpWebResponse = WebRequest.Create(faviconUrl).GetResponse() as HttpWebResponse)
                {
                    if (httpWebResponse != null && httpWebResponse.StatusCode == HttpStatusCode.OK)
                    {
                        // Log("Found a /favicon.ico file for {0}", url);
                        return faviconUrl;
                    }
                }
            }
            catch (WebException)
            {
            }

            // otherwise parse the html and look for <link rel='icon' href='' /> using html agility pack
            var htmlDocument = new HtmlWeb().Load(url.ToString());
            var links = htmlDocument.DocumentNode.SelectNodes("//link");
            if (links != null)
            {
                foreach (var linkTag in links)
                {
                    var rel = GetAttr(linkTag, "rel");
                    if (rel == null)
                        continue;

                    if (rel.Value.IndexOf("icon", StringComparison.InvariantCultureIgnoreCase) > 0)
                    {
                        var href = GetAttr(linkTag, "href");
                        if (href == null)
                            continue;

                        Uri absoluteUrl;
                        if (Uri.TryCreate(href.Value, UriKind.Absolute, out absoluteUrl))
                        {
                            // Log("Found an absolute favicon url {0}", absoluteUrl);
                            return absoluteUrl;
                        }

                        var expandedUrl = new Uri(string.Format("{0}://{1}{2}", url.Scheme, url.Host, href.Value));
                        //Log("Found a relative favicon url for {0} and expanded it to {1}", url, expandedUrl);
                        return expandedUrl;
                    }
                }
            }

            // Log("Could not find a favicon for {0}", url);
            return null;
        }
        catch
        {
            return null;
        }
    }

1 个答案:

答案 0 :(得分:0)

首先,我找到了问题的原因。我是由另一个问题造成的。

附加代码可以正常工作以获取网页的图标。

回答有关Treeview Icons的问题。不,TreeViews BUT不直接支持它们 Stackpanels支持图标,StackPanels可以是TreeView项目,因此Treeview可以支持图标。

相关问题