Click here to Skip to main content
15,884,237 members
Articles / Programming Languages / C#

Retrieve a Web Pages' Shortcut Icon

Rate me:
Please Sign up or sign in to vote.
4.50/5 (10 votes)
17 Oct 2008CPOL2 min read 108.9K   775   36   37
This article will show you a couple of ways to extract a web pages' icon
Screenshot - firefox.jpg

Screenshot - MyProj.jpg

Introduction

Ever wondered how Internet Explorer and Firefox get those little webpage icons in their address bar or tabs? Well, so did I.

I found a couple of ways to retrieve the icon from a webpage.

But, I am the first to say this surely can't be the best or easiest way of doing this. But hey, it works. Will appreciate if the gurus out there can post some alternative (by alternative, I mean better) methods of extracting the webpage SHORTCUT ICON.

The Code

This code is triggered on the webbrowser's Navigating event.

In this method, there is a double check for the webpage icon. Usually a webpage icon is called favicon.ico. So you can get the icon from a host URL (like www.google.com) by adding /favicon.ico to it. Eg. http://www.codeproject.com/favicon.ico. This should display the icon in a webbrowser.

BUT, sometimes webpages don't call their shortcut icon favicon.ico. So I went to the HTML document to retrieve the icon path. It should be located under the element tag link with attribute rel where rel equals SHORTCUT ICON.

Now I can extract the icon in two different ways. First I get the URL and simply display the icon in an HTML control, in this case a WebBrowser. Secondly, I stream the icon to an image and display it as such in an Image control.

Here's the code:

Firstly set your proxy – if any.

C#
string iconPath = "";

Image img = null;
Stream myStream = null;

WebProxy proxy = new WebProxy("http://proxy:80/", true);
proxy.Credentials = new NetworkCredential("userId", "password", "Domain");

Continuing with the above method, I simply add /favicon.ico to the current URL and send a request.

C#
WebRequest requestImg = WebRequest.Create("http://" + e.Url.Host + "/favicon.ico");

  requestImg.Proxy = proxy;
  requestImg.Timeout = 10000;

Create an instance of the WebResponse. If the response ContentLength is bigger then zero, we simply use the URL we just cooked up.

C#
WebResponse response = requestImg.GetResponse();

if (response.ContentLength > 0)
    myStream = response.GetResponseStream();

ELSE, loop through the HTML document elements and retrieve the attribute rel that equals SHORTCUT ICON. From this, we can retrieve the icon name.

C++
else
{
    HtmlDocument doc = webBrowser1.Document;
    HtmlElementCollection collect = doc.GetElementsByTagName("link");

  foreach (HtmlElement element in collect)
  {
       if (element.GetAttribute("rel") == "SHORTCUT ICON")
       iconPath = element.GetAttribute("href");
     }

  this.Text = doc.Title;

  requestImg = WebRequest.Create("http://" + e.Url.Host + iconPath);

  response = requestImg.GetResponse();

  myStream = response.GetResponseStream();
}

Lastly, I display the "values" in an Image and WebBrowser.

C++
img = Image.FromStream(myStream);
this.pictureBox1.Image = img;

webBrowser2.Url = requestImg.RequestUri;
webBrowser2.Update();

Conclusion

Like I said from the start, this surely can't be the best way, can it? Please post a message if there's any other way of doing this.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNot a web guru but... Pin
Ed.Poore11-Jun-07 1:52
Ed.Poore11-Jun-07 1:52 
GeneralRe: Not a web guru but... Pin
Johan Vorster11-Jun-07 1:59
Johan Vorster11-Jun-07 1:59 
GeneralRe: Not a web guru but... Pin
Ed.Poore11-Jun-07 2:36
Ed.Poore11-Jun-07 2:36 
GeneralRe: Not a web guru but... Pin
Johan Vorster11-Jun-07 3:00
Johan Vorster11-Jun-07 3:00 
GeneralRe: Not a web guru but... Pin
Ed.Poore11-Jun-07 3:26
Ed.Poore11-Jun-07 3:26 
GeneralRe: Not a web guru but... Pin
Johan Vorster11-Jun-07 3:51
Johan Vorster11-Jun-07 3:51 
GeneralRe: Not a web guru but... Pin
chaiguy133720-Jun-07 8:43
chaiguy133720-Jun-07 8:43 
GeneralRe: Not a web guru but... Pin
BoneSoft19-Jun-07 5:30
BoneSoft19-Jun-07 5:30 
Umm... To be XHTML compliant, you have to close the tag.

<link rel="icon" href="images/mozilla-16.png" type="image/png" />




Try code model generation tools at BoneSoft.com.

GeneralRe: Not a web guru but... Pin
Ed.Poore19-Jun-07 5:41
Ed.Poore19-Jun-07 5:41 
JokeRe: Not a web guru but... Pin
BoneSoft19-Jun-07 8:29
BoneSoft19-Jun-07 8:29 
GeneralRe: Not a web guru but... Pin
Ed.Poore19-Jun-07 8:34
Ed.Poore19-Jun-07 8:34 
QuestionRe: Not a web guru but... Pin
Miguel Azevedo e Castro20-Jun-07 22:40
Miguel Azevedo e Castro20-Jun-07 22:40 
AnswerRe: Not a web guru but... Pin
Johan Vorster20-Jun-07 22:53
Johan Vorster20-Jun-07 22:53 
AnswerRe: Not a web guru but... Pin
Ed.Poore21-Jun-07 0:39
Ed.Poore21-Jun-07 0:39 
GeneralRe: Not a web guru but... Pin
Johan Vorster21-Jun-07 1:00
Johan Vorster21-Jun-07 1:00 
GeneralRe: Not a web guru but... Pin
Ed.Poore21-Jun-07 1:12
Ed.Poore21-Jun-07 1:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.