0

The Code below extracts a Png or (ico file commented out) which when displayed by Paint displays a small 16x16 icon as expected BUT this icon file (either the png or ico does not work as a Treeview Icon . Other larger Png/Ico files do however work correctly.

     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;
        }
    }
user2840301
  • 67
  • 1
  • 1
  • 7
  • There's no such thing as a `TreeView` icon in WPF. You'd better explain your question better and show the relevant XAML. – Sheridan Feb 14 '14 at 21:46

1 Answers1

0

Firstly I have found the cause of my problem. I was caused by another problem.

The attached code does work correctly to get the icon of a Web Page.

In answer to the question about Treeview Icons. No they are not directly supported by TreeViews BUT ICONS are supported by Stackpanels and StackPanels can be TreeView Items, hence a Treeview can support Icons.

user2840301
  • 67
  • 1
  • 1
  • 7