Results 1 to 7 of 7

Thread: [RESOLVED] Getting the Favicon of a website

  1. #1
    Lively Member
    Join Date
    Oct 11
    Posts
    121

    Resolved [RESOLVED] Getting the Favicon of a website

    Hey guys!

    Can you please see my code and tell me what's wrong in it? I can't seem to find the problem:

    Code:
    public static Image favicon(Uri Url)
            {
                try
                {
                    Uri websiteuri = new Uri(Url.Host);
                    string faviconurl = "http://" + websiteuri + "/favicon.ico";
    
                    WebRequest req = WebRequest.Create(faviconurl);
    
                    WebResponse res = req.GetResponse();
    
                    Stream ico = res.GetResponseStream();
    
                    return Image.FromStream(ico);
                }
                catch (Exception ex)
                {
                    return Properties.Resources.LargeGlobe;
                }
            }
    
            private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
            {
                pictureBox5.Image = favicon(webBrowser1.Url);
    
                Form1.tabControl1.SelectedTab.Text = webBrowser1.DocumentTitle;
            }
    The code above returns the exception picture, but it doesn't get the favicon of the website.

    Need it as soon as possible.

    Thanks guys!

  2. #2
    Frenzied Member Lightning's Avatar
    Join Date
    Oct 02
    Location
    Eygelshoven
    Posts
    1,556

    Re: Getting the Favicon of a website

    The url of the favicon isn't always www.host.com/favicon.ico. You should get the default page (index.html) from the host and something like:
    Code:
    <link href="images/foo.ico" rel="shortcut icon" />
    VB6 & C# (WCF LINQ) mostly


    If you need help with a WPF/WCF question post in the NEW WPF & WCF forum and we will try help the best we can

    My site

    My blog, couding troubles and solutions

  3. #3
    Lively Member
    Join Date
    Oct 11
    Posts
    121

    Re: Getting the Favicon of a website

    @Lightning Thanks for the reply.

    I understand that all of the favicons are not located in '/favicon.ico' but Google's favicon is located there. I don't know why its not working there.

    Please Help!

  4. #4
    Lively Member
    Join Date
    Oct 11
    Posts
    121

    Re: Getting the Favicon of a website

    @Lightning Thanks for the reply.

    I understand that all of the favicons are not located in '/favicon.ico' but Google's favicon is located there. I don't know why its not working there.

    Please Help!

  5. #5
    Lively Member
    Join Date
    Oct 11
    Posts
    121

    Re: Getting the Favicon of a website

    sorry for the double post. my internet wasn't working properly.

  6. #6
    Lively Member
    Join Date
    Oct 11
    Posts
    121

    Re: Getting the Favicon of a website

    Ok. So if I want to try to get the <link href="images/foo.ico" rel="shortcut icon" /> tag, then how do I do it?

    Is there a website to get this from?

  7. #7
    Lively Member
    Join Date
    Oct 11
    Posts
    121

    Re: Getting the Favicon of a website

    Thanks for your help guys.

    I got the code off of a tutorial I finally found on Google and I just copied the code from there.

    It appears that I just had to change this:
    Code:
    public static Image favicon(Uri url)
    into this:
    Code:
    public static Image favicon(String url)
    Here's the full code:

    Code:
    public static Image favicon(String url)
            {
                Uri url = new Uri(u);
                String iconurl = "http://" + url.Host + "/favicon.ico";
    
                WebRequest request = WebRequest.Create(iconurl);
                try
                {
                    WebResponse response = request.GetResponse();
    
                    Stream s = response.GetResponseStream();
                    return Image.FromStream(s);
                }
                catch (Exception ex)
                {
                    return Properties.Resources.LargeGlobe;
                }
            }
    
            private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                pictureBox5.Image = favicon(webBrowser1.Url.ToString());
            }
    Thanks again for the help guys!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •