[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 362
  • Last Modified:

Extract Icon

I need to extract an icon from a file (usually a data file for an application) and put it into an ImageList. I did this in VB6 using APIs but am having difficulty in vb.net.

Any code to help me understand appreciated.
  • 2
1 Solution
Bob LearnedCommented:
Public Class Graphics_Library

  ' Determine how many icons a file has.
  Private Declare Function ExtractIconEx _
    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

  ' Load the icons from a file.
  Private Declare Function ExtractIcon _
    Lib "shell32.dll" Alias "ExtractIconA" _
     (ByVal hInst As IntPtr, ByVal lpszExeFileName As String, _
      ByVal nIconIndex As Integer) As Integer

  ' Release the icon handle's resources.
  Private Declare Function DestroyIcon _
    Lib "user32" _
      (ByVal hIcon As Integer) As Integer

  ' Handles to each icon.
  Private Shared m_hIcons() As Integer

  Protected Overrides Sub Finalize()


    ' Close all the icon to release their resources.

    Dim countIcons As Integer = m_hIcons.Length

    If countIcons > 0 Then

      For iconIndex As Integer = 0 To countIcons - 1


      Next iconIndex

    End If

  End Sub 'Finalize'

  Public Shared Function ExtractIcons(ByVal filePath As String) As Icon()


      ' Extract all of the regular-sized icons from the file pointed to by
      ' <filePath> to an array of Icon objects.  
      ' NOTE:  Dynamically allocated arrays are used to receive the icon handles.  

      ' Dynamic array to receive handles to the icons.

      ' Return value for API calls.
      Dim retVal As Integer

      ' Application instance handle.
      Dim hInst As IntPtr = Me.hInstance

      ' Determine how many regular icons exist in the file and resize
      ' the array accordingly.
      Dim numIcons As Integer = ExtractIconEx(filePath, -1, 0&, 0&, 0&)

      If numIcons = 0 Then
        Throw New Exception("No icons found in " & filePath)
      End If

      ' Since arrays are 0-based, then set the upper bounds to n-1.
      numIcons -= 1

      ' Resize the arrays to hold all the handles.
      ReDim m_hIcons(numIcons)

      Dim listIcons(numIcons) As Icon

      ' Loop through each icon, creating a bitmap, .
      For currentIcon As Integer = 0 To numIcons

        ' Get all of the icon handles.
        m_hIcons(currentIcon) = ExtractIcon(hInst, filePath, currentIcon)

        Dim handleIcon As New IntPtr(m_hIcons(currentIcon))

        If Not handleIcon.Equals(IntPtr.Zero) Then

          ' Get a bitmap image from the icon handle.
          listIcons(currentIcon) = Icon.FromHandle(handleIcon)

        End If

      Next currentIcon

      ' Return the array of Icon objects.
      Return listIcons

    Catch ex As Exception


    End Try

  End Function 'ExtractIcons'

  Private Shared ReadOnly Property hInstance() As IntPtr
      Return Marshal.GetHINSTANCE([Assembly].GetExecutingAssembly.GetModules()(0))
    End Get
  End Property 'hInstance'

End Class

Dim allIcons() As Icon = Graphics_Library(<path to file>)

Bob LearnedCommented:
I had to pull this from a larger application, so let me know if you have any questions or problems.

broadbentAuthor Commented:
I haven't tried it yet, but it looks very similar to my VB6 code.

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now