[RESOLVED] Rescale Favicon On The Fly
Hi,
I'm grabbing a favicon from a website and add it to a tab.
Does anyone know how to scale it? Some of the favicons are larger then 16*16, which makes it a big image on the tab.
My code to grab it:
vb.net Code:
For Each element As Gecko.GeckoElement In xGecko.Document.DocumentElement.GetElementsByTagName("link")
If element.GetAttribute("rel").ToLower = "shortcut icon" Then
result = element.GetAttribute("href")
Exit For
End If
Next
Try
Dim WebClient As New WebClient
Dim MS As New MemoryStream(WebClient.DownloadData(result))
Dim favicon As New Icon(MS)
web_tab.SelectedTab.Icon = favicon
Re: Rescale Favicon On The Fly
Quote:
Originally Posted by
Radjesh Klauke
Does anyone know how to scale it? Some of the favicons are larger then 16*16, which makes it a big image on the tab.
Maybe draw the icon as a bitmap in the new size then convert it back to an icon?
vb.net Code:
<System.Runtime.InteropServices.DllImportAttribute("user32.dll")> _
Private Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean
End Function
Private Function ResizeIcon(ByVal icon As Icon, ByVal width As Integer, ByVal height As Integer) As Icon
Using bmp As New Bitmap(width, height)
Using g As Graphics = Graphics.FromImage(bmp)
g.Clear(Color.Transparent)
g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
g.DrawIcon(icon, New Rectangle(0, 0, width, height)) ' draw icon in new size
Dim ico As Icon = icon.FromHandle(bmp.GetHicon)
Dim resizedIcon As Icon = New Icon(ico, ico.Size)
DestroyIcon(ico.Handle) ' avoid GDI memory leak
Return resizedIcon
End Using
End Using
End Function
' ~~~~~~~
if favicon.size <> new size(16,16) then
Dim newIcon As Icon = ResizeIcon(favicon, 16,16)
web_tab.SelectedTab.Icon = newIcon
newIcon.dispose()
else
web_tab.SelectedTab.Icon = favicon
end if
favicon.dispose()