Extract Icon

Posted on 2005-04-14
Last Modified: 2008-02-01
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

Any code to help me understand appreciated.
Question by:broadbent
    LVL 96

    Accepted Solution

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

    LVL 96

    Expert Comment

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

    LVL 4

    Author Comment

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

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    It’s quite interesting for me as I worked with Excel using for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now