Click here to Skip to main content
11,634,634 members (68,416 online)
Click here to Skip to main content

Retrieve a Web Pages' Shortcut Icon

, 17 Oct 2008 CPOL 67.5K 562 37
Rate this:
Please Sign up or sign in to vote.
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.

  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.

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.

  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.

  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.

  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)

Share

About the Author

Johan Vorster
Software Developer
United Kingdom United Kingdom
No Biography provided

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 2 Pin
rutstyle16-Feb-11 17:45
memberrutstyle16-Feb-11 17:45 
GeneralRe: My vote of 2 Pin
Johan "BJ" Vorster13-Jan-12 0:42
memberJohan "BJ" Vorster13-Jan-12 0:42 
GeneralA class would be nice Pin
Bigdeak22-Jun-10 5:37
memberBigdeak22-Jun-10 5:37 
QuestionHow do you save the icon? Pin
thund3rstruck13-Nov-09 17:43
memberthund3rstruck13-Nov-09 17:43 
GeneralBetter working code! Pin
mcgin159113-Jul-09 10:39
membermcgin159113-Jul-09 10:39 
GeneralRe: Better working code! Pin
Johan "BJ" Vorster13-Jul-09 22:18
memberJohan "BJ" Vorster13-Jul-09 22:18 
GeneralRe: Better working code! Pin
mcgin15919-Aug-09 12:55
membermcgin15919-Aug-09 12:55 
QuestionCode would not execute past GetResponse call... Pin
DdoubleD21-May-09 12:43
memberDdoubleD21-May-09 12:43 
AnswerRe: Code would not execute past GetResponse call... Pin
Johan "BJ" Vorster21-May-09 21:51
memberJohan "BJ" Vorster21-May-09 21:51 
GeneralRe: Code would not execute past GetResponse call... Pin
DdoubleD22-May-09 3:12
memberDdoubleD22-May-09 3:12 
GeneralRe: Code would not execute past GetResponse call... Pin
DdoubleD22-May-09 7:59
memberDdoubleD22-May-09 7:59 
GeneralRe: Code would not execute past GetResponse call... Pin
Johan "BJ" Vorster25-May-09 22:37
memberJohan "BJ" Vorster25-May-09 22:37 
GeneralRe: Code would not execute past GetResponse call... Pin
DdoubleD26-May-09 1:55
memberDdoubleD26-May-09 1:55 
GeneralRe: Code would not execute past GetResponse call... Pin
Johan "BJ" Vorster26-May-09 2:25
memberJohan "BJ" Vorster26-May-09 2:25 
Questionhow to achieve "Retreive a web pages' shortcut icon" in asp.net2.0 Pin
Mohammmed Farooq27-Aug-08 1:21
memberMohammmed Farooq27-Aug-08 1:21 
AnswerRe: how to achieve "Retreive a web pages' shortcut icon" in asp.net2.0 Pin
BJ Vorster27-Aug-08 1:32
memberBJ Vorster27-Aug-08 1:32 
Wow, it's been a while since I looked at this article. Actually a bit embarressing. Poke tongue | ;-P
Not the best article in the world, but I'll try and help you.

What exactly do you want to do in ASP.net enviroment?
GeneralRe: how to achieve "Retreive a web pages' shortcut icon" in asp.net2.0 Pin
Mohammmed Farooq27-Aug-08 17:20
memberMohammmed Farooq27-Aug-08 17:20 
GeneralRe: how to achieve "Retreive a web pages' shortcut icon" in asp.net2.0 [modified] Pin
BJ Vorster27-Aug-08 22:09
memberBJ Vorster27-Aug-08 22:09 
GeneralShould probably check for link before /favicon.ico Pin
logan133720-Jun-07 8:41
memberlogan133720-Jun-07 8:41 
GeneralRe: Should probably check for link before /favicon.ico Pin
BJ Vorster20-Jun-07 21:41
memberBJ Vorster20-Jun-07 21:41 
GeneralRe: Should probably check for link before /favicon.ico Pin
tekoteko1-Nov-07 7:47
membertekoteko1-Nov-07 7:47 
GeneralNot a web guru but... Pin
Ed.Poore11-Jun-07 1:52
memberEd.Poore11-Jun-07 1:52 
GeneralRe: Not a web guru but... Pin
Johan Vorster11-Jun-07 1:59
memberJohan Vorster11-Jun-07 1:59 
GeneralRe: Not a web guru but... Pin
Ed.Poore11-Jun-07 2:36
memberEd.Poore11-Jun-07 2:36 
GeneralRe: Not a web guru but... Pin
Johan Vorster11-Jun-07 3:00
memberJohan Vorster11-Jun-07 3:00 
GeneralRe: Not a web guru but... Pin
Ed.Poore11-Jun-07 3:26
memberEd.Poore11-Jun-07 3:26 
GeneralRe: Not a web guru but... Pin
Johan Vorster11-Jun-07 3:51
memberJohan Vorster11-Jun-07 3:51 
GeneralRe: Not a web guru but... Pin
logan133720-Jun-07 8:43
memberlogan133720-Jun-07 8:43 
GeneralRe: Not a web guru but... Pin
BoneSoft19-Jun-07 5:30
memberBoneSoft19-Jun-07 5:30 
GeneralRe: Not a web guru but... Pin
Ed.Poore19-Jun-07 5:41
memberEd.Poore19-Jun-07 5:41 
JokeRe: Not a web guru but... Pin
BoneSoft19-Jun-07 8:29
memberBoneSoft19-Jun-07 8:29 
GeneralRe: Not a web guru but... Pin
Ed.Poore19-Jun-07 8:34
memberEd.Poore19-Jun-07 8:34 
QuestionRe: Not a web guru but... Pin
fmacastro20-Jun-07 22:40
memberfmacastro20-Jun-07 22:40 
AnswerRe: Not a web guru but... Pin
BJ Vorster20-Jun-07 22:53
memberBJ Vorster20-Jun-07 22:53 
AnswerRe: Not a web guru but... Pin
Ed.Poore21-Jun-07 0:39
memberEd.Poore21-Jun-07 0:39 
GeneralRe: Not a web guru but... Pin
BJ Vorster21-Jun-07 1:00
memberBJ Vorster21-Jun-07 1:00 
GeneralRe: Not a web guru but... Pin
Ed.Poore21-Jun-07 1:12
memberEd.Poore21-Jun-07 1:12 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150728.1 | Last Updated 17 Oct 2008
Article Copyright 2007 by Johan Vorster
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid