|
-
Aug 25th, 2012, 01:24 AM
#1
Thread Starter
Lively Member
[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!
-
Aug 25th, 2012, 02:57 PM
#2
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" />
-
Aug 26th, 2012, 12:01 AM
#3
Thread Starter
Lively Member
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!
-
Aug 26th, 2012, 12:02 AM
#4
Thread Starter
Lively Member
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!
-
Aug 26th, 2012, 12:07 AM
#5
Thread Starter
Lively Member
Re: Getting the Favicon of a website
sorry for the double post. my internet wasn't working properly.
-
Aug 29th, 2012, 03:08 AM
#6
Thread Starter
Lively Member
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?
-
Sep 1st, 2012, 04:16 AM
#7
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|