Imports System.ComponentModel
Imports System.Net



Dim WithEvents WB As New WebBrowser With {.ScriptErrorsSuppressed = True}
Dim WithEvents client As New WebClient()



Sub PreserveWeb(Address As String)
Dim url As Uri = New Uri(Address)
If url.HostNameType = UriHostNameType.Dns Then
Dim URa = String.Format("{0}://{1}/", url.Scheme, url.Host)
'client.DownloadStringAsync(New Uri(URa))
WB.Navigate(URa)
End If
End Sub


Private Sub WB_DocumentCompleted(sender As Object, e As WebBrowserDocumentCompletedEventArgs) Handles WB.DocumentCompleted
Dim DomDoc As mshtml.IHTMLDocument3 = WB.Document.DomDocument
IconLoaded = False
With DomDoc
Try
'<meta Property= "og:image" content="https://learn.microsoft.com/en-us/media/open-graph-image.png" />
Dim href As String = ""
For Each link As mshtml.HTMLLinkElement In .getElementsByTagName("link")
' Debug.Print(link.outerHTML)
If IconLoaded = True Then Exit For
If IconLoaded = False AndAlso (link.getAttribute("rel").ToString.ToLower = "icon" Or link.getAttribute("rel").ToString.ToLower = "shortcut icon") Then
Debug.Print(link.getAttribute("href"))
Debug.Print(e.Url.OriginalString)
Dim urlIcon As String
If link.getAttribute("href").ToString.Contains(e.Url. Host) = True Then
urlIcon = link.getAttribute("href").ToString
Else
href = link.getAttribute("href").ToString
If href.StartsWith("/") = False Then
href = href.Insert(0, "/")
End If
urlIcon = String.Format("{0}{1}", String.Format("{0}://{1}", e.Url.Scheme, e.Url.Host), href).Replace(".com//", ".com/")
End If
Debug.Print(urlIcon)
client.DownloadDataAsync(New Uri(urlIcon))
End If
Next
For Each link As mshtml.HTMLMetaElement In .getElementsByTagName("meta")
If IconLoaded = True Then Exit For
If IconLoaded = False AndAlso (link.getAttribute("Property").ToString.ToLower = "og:image") Then
Debug.Print(link.content)
Debug.Print(e.Url.OriginalString)
Dim urlIcon As String
If link.content.Contains(e.Url.Host) = True Then
urlIcon = link.content
Else
href = link.content
If href.StartsWith("/") = False Then
href = href.Insert(0, "/")
End If
urlIcon = String.Format("{0}{1}", String.Format("{0}://{1}", e.Url.Scheme, e.Url.Host), href).Replace(".com//", ".com/")
End If
Debug.Print(urlIcon)
'client.DownloadDataAsync(New Uri(urlIcon))
client.DownloadFileAsync(New Uri(urlIcon), IO.Path.GetFullPath(".\favicon.png"))
End If
Next
Catch ex As Exception
Debug.Print(ex.Message)
End Try
End With
End Sub
Dim Strm As IO.Stream
Dim IconLoaded As Boolean = False
Dim Icon As Icon, Image As Image
Private Sub client_DownloadDataCompleted(sender As Object, e As DownloadDataCompletedEventArgs) Handles client.DownloadDataCompleted
Debug.Print(e.Result.Length)
Strm = New IO.MemoryStream(e.Result)
Try
Icon = New Icon(Strm)
ItemDialog.Icon = Icon
IconLoaded = CBool(e.Result.Length > 0)
Catch ex As Exception
Try
Dim bmp As New Bitmap(Strm)
Image = bmp
ItemDialog.PictureBox1.Image = Image
IconLoaded = CBool(e.Result.Length > 0)
Catch ex2 As Exception


End Try
End Try
End Sub


Private Sub client_DownloadFileCompleted(sender As Object, e As AsyncCompletedEventArgs) Handles client.DownloadFileCompleted
Try
Image = Image.FromFile(IO.Path.GetFullPath(".\favicon.png" ))
ItemDialog.PictureBox1.Image = Image
IconLoaded = True
Catch ex As Exception


End Try


End Sub