locked
[VB.NET 2008] How to get the favicon.ico of a webpage in a ToolStripComboBox. RRS feed

  • Question

  • (I don't know how to make good titles)

     

    Hello,

    I have a problem:

     

    I am busy with a Internet-browser like IE.

    I have a ToolStripComboBox(1) where I can put URLs in to navigate to websites, but I don't how to place the favicon.ico as icon/image on my ToolStripComboBox(1) like:

     

    1. I navigate to http://www.microsoft.com/.

    2. The program goes to get the favicon.ico on http://www.microsoft.com/favicon.ico.

    3. And then the Favicon.ico appears next (left) to the URL.

     

    I searched on MSDN, KB and Google, but I couldn't find the answer.

     

     

    Wednesday, January 2, 2008 6:34 PM

Answers

  •  Michel To wrote:

    Maybe we can take all the text in the combobox except the text after the third "/":

    http://www.google.com/search?hl=en&q=vb.net&meta=

    And then we add "favicon.ico" to it --> http://www.google.com/favicon.ico

     

    How do you program that?

     

    Hi Michel,

     

    Welcome to MSDN forums!

     

    This code sample is what you expect.

    Code Block

    Public Class Form1

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            ' Input website address into ToolStripComboBox1

            Dim url As Uri = New Uri(ToolStripComboBox1.Text)

     

            If url.HostNameType = UriHostNameType.Dns Then

                ' Get the URL of the favicon

                ' url.Host will return such string as www.google.com

                Dim iconURL = "http://" & url.Host & "/favicon.ico"

     

                ' Download the favicon

                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)

     

                ' Display the favicon on ToolStripLabel1

                Me.ToolStripLabel1.Image = favicon

     

            End If

     

        End Sub

    End Class

     

     

    Thank you Shawn's solution and cool_s' active participation.

     

    Regards,

    Martin

    Monday, January 7, 2008 6:49 AM
  • Sorry if its not good, done in hurry, a combination,

    Private Sub WebBrowser1_Navigated(ByVal sender As System.ObjectByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated 
            Dim response As System.Net.HttpWebResponse 
            Dim request As System.Net.WebRequest 
            Dim iconURL As String 
            Dim favicon As Image 
            Dim stream As System.IO.Stream 
            Dim url As Uri = WebBrowser1.Url 
            If url.HostNameType = UriHostNameType.Dns Then 
                iconURL = "http://" & url.Host & "/favicon.ico" 
                request = System.Net.HttpWebRequest.Create(iconURL) 
                Try 
                    response = request.GetResponse() 
                    stream = response.GetResponseStream() 
                Catch 
                    For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("link"
                        If element.GetAttribute("rel").ToUpper = "SHORTCUT ICON" Then 
                            Dim iconplace As String = element.GetAttribute("href"
     
                            request = System.Net.HttpWebRequest.Create(WebBrowser1.Url.ToString + iconplace) 
                            response = request.GetResponse() 
                            stream = response.GetResponseStream() 
                            favicon = Image.FromStream(stream) 
                            Me.PictureBox1.Image = favicon 
                            Exit For 
                        End If 
                    Next 
                End Try 
                If Not stream Is Nothing Then 
                    favicon = Image.FromStream(stream) 
                    Me.PictureBox1.Image = favicon 
                End If 
            End If 
     
     
            
        End Sub 


    Arjun Paudel
    Saturday, November 22, 2008 10:31 AM
  •  

    Add a ToolStripLabel1 into the ToolStrip control and set it's text as empty, set the ToolStripLabel's image by using following code:

     

     

    Code Block

            Dim iconURL = "http://www.microsoft.com/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 mimage = Image.FromStream(stream)

            Me.ToolStripLabel1.Image = mimage

     

     

    Thursday, January 3, 2008 2:46 AM

All replies

  •  

    Add a ToolStripLabel1 into the ToolStrip control and set it's text as empty, set the ToolStripLabel's image by using following code:

     

     

    Code Block

            Dim iconURL = "http://www.microsoft.com/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 mimage = Image.FromStream(stream)

            Me.ToolStripLabel1.Image = mimage

     

     

    Thursday, January 3, 2008 2:46 AM
  • Thanks,

    it worked perfectly.

    Now 3 things are left:

    • How to get the right icon of a website: Now if I go to the google homepage the microsoft icon appears but I want to get the right icon, please with a system, because I don't want to make a huge list of websites.
    • If point 1 is done, I also want to get the right icon even if I am not at the homepage, like http://www.pcreview.co.uk/forums/thread-1397771.php.
    • How to get the icon into the combobox (lesser important).
    Thursday, January 3, 2008 9:45 AM
  • I don't know for sure if this will work but it's worth a try.

     

    It's supposed to add the favicon.ico file to the URL you typed into the ToolStripComboBox.

    In order to do that you will have to make sure the code reads the typed URL and adds the /favicon.ico file automaticaly.

     

    I don't know the code for this scince I'm a beginner myself but I hope for you that someone else can help you with that.

     

    -83 cool_s -83

     

     

    Thursday, January 3, 2008 9:58 AM
  • Nope, that doesn't work:

     

    Error 404- not found

     

    Thursday, January 3, 2008 10:06 AM
  • I've got an idea:

     

    Maybe we can take all the text in the combobox except the text after the third "/":

     

    http://www.google.com/search?hl=en&q=vb.net&meta=

     

    And then we add "favicon.ico" to it --> http://www.google.com/favicon.ico

     

    But the question is:

     

    How do you program that?

     

    Thursday, January 3, 2008 12:17 PM
  •  Michel To wrote:

    Maybe we can take all the text in the combobox except the text after the third "/":

    http://www.google.com/search?hl=en&q=vb.net&meta=

    And then we add "favicon.ico" to it --> http://www.google.com/favicon.ico

     

    How do you program that?

     

    Hi Michel,

     

    Welcome to MSDN forums!

     

    This code sample is what you expect.

    Code Block

    Public Class Form1

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            ' Input website address into ToolStripComboBox1

            Dim url As Uri = New Uri(ToolStripComboBox1.Text)

     

            If url.HostNameType = UriHostNameType.Dns Then

                ' Get the URL of the favicon

                ' url.Host will return such string as www.google.com

                Dim iconURL = "http://" & url.Host & "/favicon.ico"

     

                ' Download the favicon

                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)

     

                ' Display the favicon on ToolStripLabel1

                Me.ToolStripLabel1.Image = favicon

     

            End If

     

        End Sub

    End Class

     

     

    Thank you Shawn's solution and cool_s' active participation.

     

    Regards,

    Martin

    Monday, January 7, 2008 6:49 AM
  • Reference: How to get only the domain of an Url.

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1983896&SiteID=1

     

    Monday, January 7, 2008 6:51 AM
  • How could I add one in a Picturebox1? I changed toolstripcombobox to picture box but it comes up with an error. Thanks for any help!
    Wednesday, May 7, 2008 6:32 PM
  •  RonanB wrote:
    How could I add one in a Picturebox1? I changed toolstripcombobox to picture box but it comes up with an error.

     

    Hi RonanB,

     

    To show website favicon on PictureBox, you should change ToolStripLabel1 (instead of ToolStripComboBox1) to PictureBox1.

     

    Please check this code sample:

     

    Prerequisites: TextBox1 (or ToolStripComboBox1) , PictureBox1 and Button1 on Form1.

    Code Block

    Public Class Form1

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            ' Input website address into TextBox1 or ToolStripComboBox1

            Dim url As Uri = New Uri(TextBox1.Text)

     

            If url.HostNameType = UriHostNameType.Dns Then

                ' Get the URL of the favicon

                ' url.Host will return such string as www.google.com

                Dim iconURL = "http://" & url.Host & "/favicon.ico"

     

                ' Download the favicon

                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)

     

                ' Display the favicon on PictureBox1

                Me.PictureBox1.Image = favicon

     

            End If

     

        End Sub

    End Class

     

     

    Regards,

    Martin

    Thursday, May 8, 2008 4:33 AM
  • Thanks Martin!

    That code works. I don't know why when I tried to change it to picturebox it didn't work.

     

     

    Thanks again

    Ronan

    Thursday, May 8, 2008 4:44 PM
  •  Martin Xie - MSFT wrote:

     RonanB wrote:
    How could I add one in a Picturebox1? I changed toolstripcombobox to picture box but it comes up with an error.
    To show website favicon on PictureBox, you should change ToolStripLabel1 (instead of ToolStripComboBox1) to PictureBox1.

     

     

    You should change ToolStripLabel1 (instead of ToolStripComboBox1) to PictureBox1.

    Both ToolStripLabel1 and PictureBox1 have Image property used to show the Favicon picture.

    ToolStripComboBox1 (which is used to input website URL in this scenario) doesn't have Image property.

     

    Friday, May 9, 2008 4:09 AM
  • I know this is an older thread and I don't mean to revive it, but I came across this while looking for a way to do the same. I've tried the code block provided by Martin Xie and while it does work, it doesn't work for all sites that have a favicon. How do you get the favicon for a site that links to it via a Link element in the document head? 
    Thursday, November 20, 2008 7:45 PM
  • Martin Xie - MSFT said:

    Please check this code sample:

    Prerequisites: TextBox1 (or ToolStripComboBox1) , PictureBox1 and Button1 on Form1.

    Code Block

    Public Class Form1

     

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

            ' Input website address into TextBox1 or ToolStripComboBox1

            Dim url As Uri = New Uri(TextBox1.Text)

     

            If url.HostNameType = UriHostNameType.Dns Then

                ' Get the URL of the favicon

                ' url.Host will return such string as www.google.com

                Dim iconURL = "http://" & url.Host & "/favicon.ico"

     

                ' Download the favicon

                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)

     

                ' Display the favicon on PictureBox1

                Me.PictureBox1.Image = favicon

     

            End If

     

        End Sub

    End Class

    Hi softwerx,

    My above code sample still works fine. I test it with inputing
    http://www.microsoft.comhttp://www.google.com/http://www.yahoo.com/http://www.baidu.com/http://www.cctv.com/, etc. the corresponding favicon can be retrieved and displayed on PictureBox.

    Would you like to leave your email? I will send my sample project to you for checking.


    Best regards,
    Martin Xie

    Saturday, November 22, 2008 6:18 AM
  • Hi Sofwerx!

    Yes, I had a same kind of problem, since those icons are not always called by same name and there is no guarantee of being them in root folder, another thing is though you type one url, it might forward to another and you need a favicon of that site. You need to use martin's code but if "error 404" occurs then you need to find it in Link Element. I have some code using webbrowser, look at it if it might help you.

      Private Sub WebBrowser1_Navigated(ByVal sender As System.ObjectByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated 
            For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("link"
                If element.GetAttribute("rel").ToUpper = "SHORTCUT ICON" Then 
                    Dim iconplace As String = element.GetAttribute("href"
                    Dim request As System.Net.WebRequest = System.Net.HttpWebRequest.Create(WebBrowser1.Url.ToString + iconplace) 
                    Dim response As System.Net.HttpWebResponse = request.GetResponse() 
                    Dim stream As System.IO.Stream = response.GetResponseStream() 
                    Dim favicon = Image.FromStream(stream) 
                    Me.PictureBox1.Image = favicon 
                    Exit For 
                End If 
            Next 
        End Sub 




    Arjun Paudel
    Saturday, November 22, 2008 10:15 AM
  • Sorry if its not good, done in hurry, a combination,

    Private Sub WebBrowser1_Navigated(ByVal sender As System.ObjectByVal e As System.Windows.Forms.WebBrowserNavigatedEventArgs) Handles WebBrowser1.Navigated 
            Dim response As System.Net.HttpWebResponse 
            Dim request As System.Net.WebRequest 
            Dim iconURL As String 
            Dim favicon As Image 
            Dim stream As System.IO.Stream 
            Dim url As Uri = WebBrowser1.Url 
            If url.HostNameType = UriHostNameType.Dns Then 
                iconURL = "http://" & url.Host & "/favicon.ico" 
                request = System.Net.HttpWebRequest.Create(iconURL) 
                Try 
                    response = request.GetResponse() 
                    stream = response.GetResponseStream() 
                Catch 
                    For Each element As HtmlElement In WebBrowser1.Document.GetElementsByTagName("link"
                        If element.GetAttribute("rel").ToUpper = "SHORTCUT ICON" Then 
                            Dim iconplace As String = element.GetAttribute("href"
     
                            request = System.Net.HttpWebRequest.Create(WebBrowser1.Url.ToString + iconplace) 
                            response = request.GetResponse() 
                            stream = response.GetResponseStream() 
                            favicon = Image.FromStream(stream) 
                            Me.PictureBox1.Image = favicon 
                            Exit For 
                        End If 
                    Next 
                End Try 
                If Not stream Is Nothing Then 
                    favicon = Image.FromStream(stream) 
                    Me.PictureBox1.Image = favicon 
                End If 
            End If 
     
     
            
        End Sub 


    Arjun Paudel
    Saturday, November 22, 2008 10:31 AM
  • Thank you Arjun for your considerate supplement and friendly help!
    Tuesday, November 25, 2008 6:22 AM
  • How would you do this for http://www.myyearbook.com because that doesn't work for their site.....
    Sunday, March 29, 2009 3:23 AM
  • How would you do this for http://www.myyearbook.com because that doesn't work for their site.....


    Please try another idea:
    Display the online Favicon on WebBrowser, instead of downloading and displaying it on PictureBox.

    Prerequisites: TextBox1, WebBrowser1 and Button1 on Form1.
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            ' Input website address into TextBox1
            Dim url As Uri = New Uri(TextBox1.Text)
            If url.HostNameType = UriHostNameType.Dns Then
                ' Get the URL of the favicon
                ' url.Host will return such string as www.google.com
                Dim iconURL = "http://" & url.Host & "/favicon.ico"
    
                'Display the online icon on WebBrowser
                WebBrowser1.AutoSize = True
                WebBrowser1.ScrollBarsEnabled = False
                WebBrowser1.Url = New Uri(iconURL)
                ' or WebBrowser1.Navigate(iconURL)
            End If
    
        End Sub
    
    End Class
    Tuesday, March 31, 2009 8:43 AM
  • How do you display the favicon in a combo box? Thanks in advanced....
    Wednesday, April 1, 2009 2:13 PM
  • How do you display the favicon in a combo box? Thanks in advanced....

    You can display an image beside each item in ComboBox as below code sample:

    Public Class Form1
    
        Dim ImageList1 As New ImageList
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ' Add images to ImageList1
            ImageList1.Images.Add(0, Image.FromFile("D:\VBProject\1.jpg"))
            ImageList1.Images.Add(0, Image.FromFile("D:\VBProject\2.jpg"))
            ImageList1.Images.Add(0, Image.FromFile("D:\VBProject\3.gif"))
    
            ' Add items to ComboBox1
            For i As Int32 = 0 To ImageList1.Images.Count - 1
                ComboBox1.Items.Add("Item " & i.ToString)
            Next
    
            ComboBox1.DrawMode = DrawMode.OwnerDrawVariable
            ComboBox1.ItemHeight = ImageList1.ImageSize.Height
            'ComboBox1.Width = ImageList1.ImageSize.Width + 18
            'ComboBox1.MaxDropDownItems = ImageList1.Images.Count
    
        End Sub
    
        Private Sub ComboBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ComboBox1.DrawItem
            If e.Index <> -1 Then
                e.Graphics.DrawImage(ImageList1.Images(e.Index), e.Bounds.Left, e.Bounds.Top)
                e.Graphics.DrawString(ComboBox1.Items(e.Index), New Font("Arial", 10), New SolidBrush(Color.Black), e.Bounds.Left + 20, e.Bounds.Top)
            End If
        End Sub
    
        Private Sub ComboBox1_MeasureItem(ByVal sender As Object, ByVal e As System.Windows.Forms.MeasureItemEventArgs) Handles ComboBox1.MeasureItem
            e.ItemHeight = ImageList1.ImageSize.Height
            e.ItemWidth = ImageList1.ImageSize.Width
        End Sub
    
    End Class

    Here is the illustration:
    Thursday, April 2, 2009 9:51 AM