Click here to Skip to main content
6,629,885 members and growing! (20,456 online)
Email Password   helpLost your password?
General Programming » Internet / Network » General     Intermediate License: The Code Project Open License (CPOL)

Retrieve a Web Pages' Shortcut Icon

By Johan "BJ" Vorster

This article will show you a couple of ways to extract a web pages' icon
C# 2.0, Windows, .NET 2.0VS2005, Dev
Version:2 (See All)
Posted:11 Jun 2007
Updated:17 Oct 2008
Views:26,761
Bookmarked:29 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
7 votes for this article.
Popularity: 2.82 Rating: 3.33 out of 5
1 vote, 14.3%
1

2

3
3 votes, 42.9%
4
3 votes, 42.9%
5
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)

About the Author

Johan "BJ" Vorster


Member

Occupation: Software Developer
Location: United Kingdom United Kingdom

Other popular Internet / Network articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 34 (Total in Forum: 34) (Refresh)FirstPrevNext
GeneralHow do you save the icon? Pinmemberthund3rstruck18:43 13 Nov '09  
GeneralBetter working code! Pinmembermcgin159111:39 13 Jul '09  
GeneralRe: Better working code! PinmemberJohan "BJ" Vorster23:18 13 Jul '09  
GeneralRe: Better working code! Pinmembermcgin159113:55 9 Aug '09  
QuestionCode would not execute past GetResponse call... PinmemberDdoubleD13:43 21 May '09  
AnswerRe: Code would not execute past GetResponse call... PinmemberJohan "BJ" Vorster22:51 21 May '09  
GeneralRe: Code would not execute past GetResponse call... PinmemberDdoubleD4:12 22 May '09  
GeneralRe: Code would not execute past GetResponse call... PinmemberDdoubleD8:59 22 May '09  
GeneralRe: Code would not execute past GetResponse call... PinmemberJohan "BJ" Vorster23:37 25 May '09  
GeneralRe: Code would not execute past GetResponse call... PinmemberDdoubleD2:55 26 May '09  
GeneralRe: Code would not execute past GetResponse call... PinmemberJohan "BJ" Vorster3:25 26 May '09  
Generalhow to achieve "Retreive a web pages' shortcut icon" in asp.net2.0 PinmemberMohammmed Farooq2:21 27 Aug '08  
GeneralRe: how to achieve "Retreive a web pages' shortcut icon" in asp.net2.0 PinmemberBJ Vorster2:32 27 Aug '08  
GeneralRe: how to achieve "Retreive a web pages' shortcut icon" in asp.net2.0 PinmemberMohammmed Farooq18:20 27 Aug '08  
GeneralRe: how to achieve "Retreive a web pages' shortcut icon" in asp.net2.0 [modified] PinmemberBJ Vorster23:09 27 Aug '08  
GeneralShould probably check for link before /favicon.ico Pinmemberlogan13379:41 20 Jun '07  
GeneralRe: Should probably check for link before /favicon.ico PinmemberBJ Vorster22:41 20 Jun '07  
GeneralRe: Should probably check for link before /favicon.ico Pinmembertekoteko8:47 1 Nov '07  
Probably should check for

if (element.GetAttribute("rel").ToLower() == "shortcut icon" || element.GetAttribute("rel").ToLower() == "icon" || element.GetAttribute("type").ToLower() == "image/x-icon")
Sign In·View Thread·PermaLink4.00/5 (2 votes)
GeneralNot a web guru but... PinmemberEd.Poore2:52 11 Jun '07  
GeneralRe: Not a web guru but... PinmemberJohan Vorster2:59 11 Jun '07  
GeneralRe: Not a web guru but... PinmemberEd.Poore3:36 11 Jun '07  
GeneralRe: Not a web guru but... PinmemberJohan Vorster4:00 11 Jun '07  
GeneralRe: Not a web guru but... PinmemberEd.Poore4:26 11 Jun '07  
GeneralRe: Not a web guru but... PinmemberJohan Vorster4:51 11 Jun '07  
GeneralRe: Not a web guru but... Pinmemberlogan13379:43 20 Jun '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 17 Oct 2008
Editor: Deeksha Shenoy
Copyright 2007 by Johan "BJ" Vorster
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project