Extract Icon

I would like to extract an Icon from a file to display it in a picturebox; is there simple code to do this   I've tried searching VB, but I can't make the examples work!

Thanks


Answer this question

Extract Icon

  • pollie32

    Thank You very much!

  • arclsvg

    '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("""", "")

            'Open the 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

            If intIcon <> 0 Then
                Dim objIcon As Icon
                Dim stream As New IO.MemoryStream
                objIcon = GetIconFH(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

        Private Shared Function GetIconFH(ByVal inticon As IntPtr) As Icon
            GetIconFH = Icon.FromHandle(inticon)
        End Function


    ExtractIcon is defined as:

    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


    Enjoy... this works well. (if I've remembered all the dependencies)

    If you are storing these icons .. and if you can set the number of color bits, be sure to set the storage container such as an imagelist control to 24 or better yet 32 bits. If you don;t you'll be disapointed.



  • al_cenov

    Thanks.  I'll give the code a try!!  Been bvery busy lately.
  • Extract Icon