Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Extracting Icons from and exe or dll

 

Does anyone know of a function' date=' method, namespace anything I could look into/use to extract icons from within the VBExpress ide or vb.net in general?[/quote']

 

 

hi,

 

u can use the ExtractIcon win32 API... it takes the instance handle to the dll or exe and some other parameters.. look out the documentation urself..

 

bye

Posted
I was wanting to know how to do this in the .NET FrameWork 1.1 or 2.0.

 

Knowing you wanted the .net way to get icons from any file type I will post the not so VB.net way to get the job done. Nowhere have I found a way to do it without api.

 

This is a part of my RegisteredFileTypes Class that is the reason why there can be an index behind the path. The registry stores the paths in that manner. If you want it out you should be able too.

 

 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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...