Recuperando favicon como icono en lugar de imagen

I used the favicon-code I found aquí 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.

Gracias a JP Hellemons este código funciona:

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;
}

preguntado el 01 de febrero de 12 a las 22:02

Re your "Visual C#" question: C# is C#. I guess you can say "Visual" if you use the Visual Studio IDE mostly in graphic design mode, but there is no "Visual C#" vs "Other kinds of C#", like there is with Basic. -

@MrLister: OK thanks, It's because i've red a lot of tuts wich mention Visual C# -

2 Respuestas

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

Usé este artículo: 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?

Edición final: (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;
}

Respondido 08 Feb 12, 14:02

Thanks for the quick answer! I've tweeked you code a little bit, as returning wouldn't work (I had to create a new Icon) + I wanted to use my own favicon in case the site didn't have one. Sadly, I still get an error: Argument picture must be an image that can be used as an Icon See my code in the next post - BlueCacti

The new version gives me this error in VS: Cannot implicitly convert type 'System.Drawing.Image' to 'System.Drawing.Bitmap'. An explicit conversion exists (are you missing a cast?) - BlueCacti

Usar (Bitmap)Bitmap.FromStream(ms); and a MessageBox in the catch results in the following error: Invalid parameter at System.Drawing.Image.FromStream(....) ... on rule 340 Rule 339-341: 'using (MemoryStream ms = new MemoryStream()) { bm = (Bitmap)Bitmap.FromStream(ms); }' - BlueCacti

That is because the ico contains multiple bitmaps, more about it here: channel9.msdn.com/Forums/TechOff/… - JP Hellemons

Works like a charm! Thank you VERY much. Hartelijk dank! - BlueCacti

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.

Respondido 02 Feb 12, 03:02

Hi, thanks for the help. But I don't now much about working with Streams etc. I'm a beginner in C#. Could you help me out with some code examples? I'm not asking for the full code (but I won't stop you from doing it anyway :p), but some code would be usefull - BlueCacti

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.