Results 1 to 2 of 2

Thread: [RESOLVED] Rescale Favicon On The Fly

  1. #1

    Thread Starter
    PowerPoster Radjesh Klauke's Avatar
    Join Date
    Dec 2005
    Location
    Sexbierum (Netherlands)
    Posts
    2,244

    Resolved [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:
    1. For Each element As Gecko.GeckoElement In xGecko.Document.DocumentElement.GetElementsByTagName("link")
    2.                 If element.GetAttribute("rel").ToLower = "shortcut icon" Then
    3.                     result = element.GetAttribute("href")
    4.                     Exit For
    5.                 End If
    6.             Next
    7.             Try
    8.                 Dim WebClient As New WebClient
    9.                 Dim MS As New MemoryStream(WebClient.DownloadData(result))
    10.                 Dim favicon As New Icon(MS)
    11.  
    12.                 web_tab.SelectedTab.Icon = favicon


    If you found my post helpful, please rate it.

    Codebank Submission: FireFox Browser (Gecko) in VB.NET, Load files, (sub)folders treeview with Windows icons

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Rescale Favicon On The Fly

    Quote Originally Posted by Radjesh Klauke View Post
    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:
    1. <System.Runtime.InteropServices.DllImportAttribute("user32.dll")> _
    2. Private Shared Function DestroyIcon(ByVal handle As IntPtr) As Boolean
    3. End Function
    4.  
    5. Private Function ResizeIcon(ByVal icon As Icon, ByVal width As Integer, ByVal height As Integer) As Icon
    6.     Using bmp As New Bitmap(width, height)
    7.         Using g As Graphics = Graphics.FromImage(bmp)
    8.             g.Clear(Color.Transparent)
    9.             g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic
    10.             g.DrawIcon(icon, New Rectangle(0, 0, width, height)) ' draw icon in new size
    11.             Dim ico As Icon = icon.FromHandle(bmp.GetHicon)
    12.             Dim resizedIcon As Icon = New Icon(ico, ico.Size)
    13.             DestroyIcon(ico.Handle) ' avoid GDI memory leak
    14.             Return resizedIcon
    15.         End Using
    16.     End Using
    17. End Function
    18.  
    19. ' ~~~~~~~
    20.  
    21. if favicon.size <> new size(16,16) then
    22.    Dim newIcon As Icon = ResizeIcon(favicon, 16,16)  
    23.    web_tab.SelectedTab.Icon = newIcon
    24.    newIcon.dispose()  
    25. else
    26.    web_tab.SelectedTab.Icon = favicon
    27. end if
    28.  
    29. favicon.dispose()

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width