Retrieving favicon as icon instead of image

孤者浪人 提交于 2019-12-13 12:19:46

问题


I used the favicon-code I found here to retrieve the favicon of the site loaded in the browser element.

I want to use this favicon as the icon of my Windows Form.

Thanks to JP Hellemons this code works:

private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
    this.Icon = favicon(GetActiveBrowser().Url);
}

private WebBrowser GetActiveBrowser() {
    return (WebBrowser)tabs.SelectedTab.Controls[0];
}

private Icon favicon(Uri url) {
    WebRequest request = (HttpWebRequest)WebRequest.Create("http://" + url.Host + "/favicon.ico");

    Bitmap bm = new Bitmap(32, 32);
    MemoryStream memStream;

    using (Stream response = request.GetResponse().GetResponseStream()) {
        memStream = new MemoryStream();
        byte[] buffer = new byte[1024];
        int byteCount;

        do {
            byteCount = response.Read(buffer, 0, buffer.Length);
            memStream.Write(buffer, 0, byteCount);
        } while (byteCount > 0);
    }

    bm = new Bitmap(Image.FromStream(memStream));

    if (bm != null) {
        Icon ic = Icon.FromHandle(bm.GetHicon());
        return ic;
    } else
        return Properties.Resources.GZbrowser;
}

回答1:


According to this documentation, it should work from stream. http://msdn.microsoft.com/en-us/library/system.drawing.icon.aspx

I used this article: http://odetocode.com/Blogs/scott/archive/2004/10/05/webrequest-and-binary-data.aspx

WebRequest request = (HttpWebRequest)WebRequest.Create("http://" + url.Host + "/favicon.ico");
Icon ic = new Icon(); // put default here
Bitmap bm = new Bitmap();

try
{
    using(WebResponse response = request.GetResponse())
    {
       using(Stream responseStream = response.GetResponseStream())
       {
          using(MemoryStream ms = new MemoryStream())
          {
              var tmp = Image.FromStream(ms); // changed bitmap to image
              bm = new Bitmap(tmp);
          }
       }
    }
}catch{}
if(bm != null)
{
    ic = Icon.FromHandle(bm.GetHicon);  
}
return ic;

Edit: something like this should do it

Edit2: changed some things in the answer. Can you try this?

Final edit: (lol)

Just tested this in a windows form app and this works! :)

Uri url = new Uri("http://www.google.nl");
WebRequest request = (HttpWebRequest)WebRequest.Create("http://" + url.Host + "/favicon.ico");

Bitmap bm = new Bitmap(32,32); 
MemoryStream memStream;

using (Stream response = request.GetResponse().GetResponseStream())
{
    memStream = new MemoryStream();
    byte[] buffer = new byte[1024];
    int byteCount;

    do
    {
        byteCount = response.Read(buffer, 0, buffer.Length);
        memStream.Write(buffer, 0, byteCount);
    } while (byteCount > 0);
}

bm = new Bitmap(Image.FromStream(memStream));                 

if (bm != null) 
{
    Icon ic = Icon.FromHandle(bm.GetHicon());
    this.Icon = ic;
}



回答2:


Read response byte array first, than create MemoryStream of it and create icon from that MemoryStream.

Network stream do not support seek operations that seem to be necessary for creating an icon.



来源:https://stackoverflow.com/questions/9104653/retrieving-favicon-as-icon-instead-of-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!