These little functions will load icons from any file type. Nowhere have I found a way to do it without api.

These are part of my RegisteredFileTypes Class that is the reason why there can be an index behind the path. The registry stores the paths in that manner. If you want it out you should be able too.

VB Code:
  1. Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconExA" _
  2.             (ByVal lpszFile As String, _
  3.              ByVal nIconIndex As Integer, _
  4.              ByRef phiconLarge As Integer, _
  5.              ByRef phiconSmall As Integer, _
  6.              ByVal nIcons As Long) As Integer
  7.  
  8.     'calls API to determine how many icons are embedded in the file
  9.     'vPath can contain an index number. It must be separated by a comma
  10.     Private Function GetNumberOfIcons(ByVal vPath As String) As Integer
  11.         Dim strfile As String
  12.         If vPath.IndexOf(",") > 0 Then
  13.             strfile = vPath.Substring(0, vPath.IndexOf(","))
  14.         Else
  15.             strfile = vPath
  16.         End If
  17.         strfile = strfile.Replace("""", "")
  18.         Return ExtractIcon(strfile, -1, 0, 0, 0)
  19.     End Function
  20.  
  21.     'Loads Icon from any file.
  22.     'vPath can contain an index number. It must be separated by a comma
  23.     Private Function GetIcon(ByVal vPath As String) As Icon
  24.         Dim strfile As String
  25.         Dim intIndex, intIcon, intIcon2 As Integer
  26.  
  27.         'Split path in index and path
  28.         If vPath.IndexOf(",") > 0 Then
  29.             strfile = vPath.Substring(0, vPath.IndexOf(","))
  30.             intIndex = Convert.ToInt32(vPath.Substring(vPath.IndexOf(",") + 1))
  31.         Else
  32.             strfile = vPath
  33.             intIndex = 0
  34.         End If
  35.  
  36.         'If path contains dubblequotes remove them
  37.         strfile = strfile.Replace("""", "")
  38.  
  39.         'try to open file
  40.         Dim fi As System.IO.FileInfo
  41.         Try
  42.             fi = New System.IO.FileInfo(strfile)
  43.         Catch ex As Exception
  44.             Console.WriteLine("Bad filename: " & strfile)
  45.             Console.WriteLine(ex.ToString)
  46.             Return Nothing
  47.         End Try
  48.  
  49.         'Try To extract Icon at index
  50.         ExtractIcon(strfile, intIndex, intIcon, intIcon2, 1)
  51.  
  52.         'If this failes take the first icon
  53.         If intIcon = 0 Then
  54.             ExtractIcon(strfile, 0, intIcon, intIcon2, 1)
  55.         End If
  56.  
  57.         'Console.WriteLine("Icon: " & strfile)
  58.         'Console.WriteLine("Index: " & intIndex)
  59.         'Console.WriteLine("Data:" & intIcon & "," & intIcon2)
  60.  
  61.         If intIcon <> 0 Then
  62.             Dim objIcon As Icon
  63.  
  64.             'Load Icon to memory
  65.             'NOTE: STOLEN FROM MISC OTHER EXAMPLES:
  66.             '      I have no idea how this works...
  67.             '      Looks like I need a lesson in the MemoryStream
  68.             '      Object
  69.  
  70.             Dim stream As New IO.MemoryStream
  71.             objIcon = Icon.FromHandle(New IntPtr(intIcon))
  72.             Application.DoEvents()
  73.             objIcon.Save(stream)
  74.             Dim intLength As Integer = CType(stream.Length, Integer)
  75.             Dim b(intLength) As Byte
  76.             stream.Position = 0
  77.             stream.Read(b, 0, intLength)
  78.             stream.Close()
  79.  
  80.             'DestroyIcon(intIcon)
  81.  
  82.             Return DirectCast(objIcon.Clone, Icon)
  83.         Else
  84.             Return Nothing
  85.         End If
  86.     End Function