How to get count of stored icons into a file?

时间:2018-02-03 08:04:08

标签: vb.net

Into a project that I am working on, there is a function by the name "ReturnIcon" which extracts icons from .exe, .dll, .ico etc and return them to bitmap.

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 Integer) As Integer

Public Function ReturnIcon(ByVal Path As String, ByVal Index As Integer, Optional ByVal small As Boolean = False) As Icon
    Dim bigIcon As Integer
    Dim smallIcon As Integer

    ExtractIcon(Path, Index, bigIcon, smallIcon, 1)
    If bigIcon = 0 Then
        ExtractIcon(Path, 0, bigIcon, smallIcon, 1)
    End If

    If bigIcon <> 0 Then
        If small = False Then
            Return Icon.FromHandle(bigIcon)
        Else
            Return Icon.FromHandle(smallIcon)
        End If
    Else
        Return Nothing
    End If
End Function

And you can use it like this:

X_Control.Image = ReturnIcon(IconPath, 0, 0).ToBitmap

I would like to add the ability so I can get count of stored icons for selected file. There is anyway to add it into this function? Should I make a new one for this? Any example would be really appreciatable!

1 个答案:

答案 0 :(得分:0)

答案在我的鼻子底下。我只需这样做......

Public 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 Integer) As Integer

Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim count As Integer = ExtractIcon(FilePath, -1, Nothing, Nothing, 0)
    NumericUpDown.Maximum = count
End Sub