Extract Icon

Ever wanted to get the icon that a file uses? This simple Extract_Icon function shows you how...

Private Const SHGFI_DISPLAYNAME = &H200
Private Const SHGFI_ICON = &H100
Private Const SHGFI_SYSICONINDEX = &H4000                '  get system icon index
Private Const SHGFI_LARGEICON = &H0                      '  get large icon
Private Const SHGFI_SMALLICON = &H1                      '  get small icon
Private Const ILD_TRANSPARENT = &H1
Public Const MAX_PATH = 260
Private Type SHFILEINFO 'Structure used by SHGetFileInfo
   hIcon As Long
   iIcon As Long
   dwAttributes As Long
   szDisplayName As String * MAX_PATH
   szTypeName As String * 80
End Type
Private Declare Function ImageList_Draw Lib "comctl32.dll" (ByVal himl&, ByVal i&, ByVal hDCDest&, ByVal x&, ByVal y&, ByVal flags&) As Long
Private Declare Function SHGetFileInfo Lib "shell32.dll" Alias "SHGetFileInfoA" (ByVal pszPath As String, ByVal dwFileAttributes As Long, psfi As SHFILEINFO, ByVal cbFileInfo As Long, ByVal uFlags As Long) As Long

Public Function ExtractIcon(picImage As PictureBox, sFile As String)
Dim himl As Long
Dim lpzxExeName As String
Dim lRet As Long
Dim sWinPath As String
Dim shinfo As SHFILEINFO

lpzxExeName = sFile
himl = SHGetFileInfo(lpzxExeName, 0&, shinfo, Len(shinfo), SHGFI_SYSICONINDEX Or SHGFI_SMALLICON)
If himl <> 0 Then
    picImage.AutoRedraw = True
    lRet = ImageList_Draw(himl, shinfo.iIcon, picImage.hDC, 0, 0, ILD_TRANSPARENT)
    picImage.Refresh
    ExtractIcon = True
End If
End Function

You might also like...

Comments

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“You can stand on the shoulders of giants OR a big enough pile of dwarfs, works either way.”