C# 将HTML网页中的图标提取到文件(png或ico)中,作为一个视图图标加载

C# 将HTML网页中的图标提取到文件(png或ico)中,作为一个视图图标加载,c#,wpf,url,treeview,ico,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

下面的代码提取了一个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;
        }
    }
公共静态bool GetURLIconFile(字符串webpageUrl,字符串IconFile)
{
//返回IconFile中webpageURL的图标
string siteUrl=GetWebSite(webpageUrl);//只返回站点的URL
var url=GetURLIcon(“http://”+siteUrl);
如果(url==null)
{
删除文件(IconFile);
返回false;
}
尝试
{
HttpWebRequest w=(HttpWebRequest)HttpWebRequest.Create(url);
w、 AllowAutoRedirect=true;
HttpWebResponse r=(HttpWebResponse)w.GetResponse();
System.Drawing.Image ico;
使用(流s=r.GetResponseStream())
{
ico=系统.Drawing.Image.FromStream(s);
保存(IconFile,System.Drawing.Imaging.ImageFormat.Png);//forpng
//保存(IconFile,System.Drawing.Imaging.ImageFormat.ico);//forico
}
返回true;
}
接住
{
删除文件(IconFile);
返回false;
} 
}
公共静态Uri GetURLIcon(字符串siteUrl)
{
//首先尝试查找a/favicon.ico
尝试
{
var url=新的Uri(siteUrl);
var faviconUrl=新Uri(string.Format(“{0}://{1}/favicon.ico”,url.Scheme,url.Host));
尝试
{
使用(var httpWebResponse=WebRequest.Create(faviconUrl.GetResponse()作为httpWebResponse)
{
if(httpWebResponse!=null&&httpWebResponse.StatusCode==HttpStatusCode.OK)
{
//日志(“找到{0}的/favicon.ico文件”,url);
返回faviconUrl;
}
}
}
捕获(WebException)
{
}
//否则,解析html并寻找使用html敏捷包的方法
var htmlDocument=new HtmlWeb().Load(url.ToString());
var links=htmlDocument.DocumentNode.SelectNodes(“//link”);
如果(链接!=null)
{
foreach(链接中的var linkTag)
{
var rel=GetAttr(linkTag,“rel”);
如果(rel==null)
持续
if(rel.Value.IndexOf(“icon”,StringComparison.InvariantCultureIgnoreCase)>0)
{
var href=GetAttr(linkTag,“href”);
如果(href==null)
持续
Uri绝对URL;
if(Uri.TryCreate(href.Value、UriKind.Absolute、out absoluteUrl))
{
//日志(“找到一个绝对favicon url{0}”,绝对url);
返回绝对URL;
}
var expandedUrl=newURI(string.Format(“{0}://{1}{2}”、url.Scheme、url.Host、href.Value));
//日志(“找到{0}的相对favicon url并将其扩展到{1}”,url,expandedUrl);
返回expandedUrl;
}
}
}
//日志(“找不到{0}的favicon”,url);
返回null;
}
接住
{
返回null;
}
}

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

附加的代码可以正确地获取网页的图标

回答关于Treeview图标的问题。不,Treeview不直接支持图标,但是
Stackpanels支持图标,Stackpanels可以是TreeView项目,因此TreeView可以支持图标。

WPF中没有
TreeView
图标。您最好更好地解释您的问题并显示相关的XAML。