Tip How to get favicon in tabbed webbrowser. Tutorial

stianxxs

Member
Joined
Jun 25, 2010
Messages
7
Location
Norway
Programming Experience
3-5
Hi, many of you are may working on some sort of project with web browser related settings or information, so to day I'm gonna demonstrate how to get favicon in a tabbed web browser! It's is actually quite simple. But I've been searching for this "How to get favicon in web browser" and i found many answers, but not the right one. because in all the samples I've been seeing you can't type i.e "http://www.google.com" you need to type "www.google.com" because you need to add "http://" in the source of the request! But I'm gonna show you how to get your user to type "http://www.google.com" or "www.google.com" because the web browser need "http://" to get response from the server!

So first we need to declare our settings

VB.NET:
CType(TabControl1.SelectedTab.Controls.Item(0), WebBrowser).Navigate(txtAdress.Text)

We get our web browser to go to the url from txtAdress

VB.NET:
Dim stringadress As String = txtAdress.Text
        Dim outstring() As String
        Dim Seps() As Char = {"h", "t", "t", "p", ":", "/", "/"}
We declare where we want to split the URL, and this is how we don't get stuck by typing "http://" in every URL!

VB.NET:
Try
            outstring = stringadress.Split("h", "t", "t", "p", ":", "/", "/")
            For i = 0 To outstring.Length - 1
                outstring = stringadress.Split(Seps, StringSplitOptions.RemoveEmptyEntries)
                MsgBox(outstring(i))
                'We send the splitted string to a label that corrects it
                txtAdressCheck.Text = outstring(i)
                txtAdress.Text = "http://" & txtAdressCheck.Text 'We have removed the "http://" earlier and can now safe add "http://" to every splitted URL
"txtAdre
We get our web browser to TRY to split the string txtAdress.text that we declared earlier.

VB.NET:
Dim url As Uri = New Uri(txtAdress.Text)
                If url.HostNameType = UriHostNameType.Dns Then

Checks the string if it have the "http://" in front of the URL

VB.NET:
Dim browser As New WebBrowser
                    Dim iconURL = "http://" & txtAdressCheck.Text & "/favicon.ico"
                    Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(iconURL)
                    Dim response As System.Net.HttpWebResponse = request.GetResponse()
                    Dim stream As System.IO.Stream = response.GetResponseStream()
                    Dim favicon = Image.FromStream(stream)
                    'Fav Icon
                    ImgFavIco.Image = favicon

And finally gets the requested favicon in our imagebox!

Did this help? or not?
Leave a comment! :)

Sorry for my bad English! I'm from Norway
 
I wanted to say thanks for a Great Tutorial, But your method by Splitting the http is not correct! There were many Webpages that was splitted wrong.

One particular is YOUTUBE.COM! For example using the following Urls below will all show the result as "YOU":
YouTube, or youtube.com, or YouTube... The result is "YOU"!

Plus I dont think the textbox here is nessesary : txtAdress.Text , Usually people would like to see what they have typed in, and what the result is.

Else very good job I like the Tutorial.
 
Back
Top