Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconExA" _
(ByVal lpszFile As String, _
ByVal nIconIndex As Integer, _
ByRef phiconLarge As Integer, _
ByRef phiconSmall As Integer, _
ByVal nIcons As Long) As Integer
calls API to determine how many icons are embedded in the file
vPath can contain an index number. It must be separated by a comma
Private Function GetNumberOfIcons(ByVal vPath As String) As Integer
Dim strfile As String
If vPath.IndexOf(",") > 0 Then
strfile = vPath.Substring(0, vPath.IndexOf(","))
Else
strfile = vPath
End If
strfile = strfile.Replace("""", "")
Return ExtractIcon(strfile, -1, 0, 0, 0)
End Function
Loads Icon from any file.
vPath can contain an index number. It must be separated by a comma
Private Function GetIcon(ByVal vPath As String) As Icon
Dim strfile As String
Dim intIndex, intIcon, intIcon2 As Integer
Split path in index and path
If vPath.IndexOf(",") > 0 Then
strfile = vPath.Substring(0, vPath.IndexOf(","))
intIndex = Convert.ToInt32(vPath.Substring(vPath.IndexOf(",") + 1))
Else
strfile = vPath
intIndex = 0
End If
If path contains dubblequotes remove them
strfile = strfile.Replace("""", "")
try to open file
Dim fi As System.IO.FileInfo
Try
fi = New System.IO.FileInfo(strfile)
Catch ex As Exception
Console.WriteLine("Bad filename: " & strfile)
Console.WriteLine(ex.ToString)
Return Nothing
End Try
Try To extract Icon at index
ExtractIcon(strfile, intIndex, intIcon, intIcon2, 1)
If this failes take the first icon
If intIcon = 0 Then
ExtractIcon(strfile, 0, intIcon, intIcon2, 1)
End If
Console.WriteLine("Icon: " & strfile)
Console.WriteLine("Index: " & intIndex)
Console.WriteLine("Data:" & intIcon & "," & intIcon2)
If intIcon <> 0 Then
Dim objIcon As Icon
Load Icon to memory
NOTE: STOLEN FROM MISC OTHER EXAMPLES:
I have no idea how this works...
Looks like I need a lesson in the MemoryStream
Object
Dim stream As New IO.MemoryStream
objIcon = Icon.FromHandle(New IntPtr(intIcon))
Application.DoEvents()
objIcon.Save(stream)
Dim intLength As Integer = CType(stream.Length, Integer)
Dim b(intLength) As Byte
stream.Position = 0
stream.Read(b, 0, intLength)
stream.Close()
DestroyIcon(intIcon)
Return DirectCast(objIcon.Clone, Icon)
Else
Return Nothing
End If
End Function