+ Reply to Thread
Results 1 to 7 of 7

Thread: Extracting Icon from .exe

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Extracting Icon from .exe

    In this tutorial I will show you how to easily extract the Icons from a .exe file with VB.NET.






    First we need to add this, OBSERVE that this is in the Class, not outside, this is not an import.

    Code:
    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
    This is what we use to get the handle from the icons so we can create the icons.





    Create a new function:

    Code:
        Private 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
    Path is the path where the .exe is on your computer, Index is which of the exe file's icons we want starting at 0, we will create the program so if it fail to get the wanted icon it will try to get the icon at index 0 instead. Small is optional and will make the function return the small version of the icon instead. bigIcon and smallIcon is not really the icons, they are integers and will be used to create the Icon we want to return.







    Try to extract icon with the right path and index:


    Code:
    ExtractIcon(Path, Index, bigIcon, smallIcon, 1)

    If this worked, bigIcon and smallIcon have now changed in value.





    If it didn't work it's still 0, and if that's the case:

    Code:
            If bigIcon = 0 Then
                ExtractIcon(Path, 0, bigIcon, smallIcon, 1)
            End If
    ...try to get the icon at index 0.






    If one of the above thing worked we can use one of the integers to get the icon we want, else return nothing:

    Code:
            If bigIcon <> 0 Then
                If small = False Then
                    Return Icon.FromHandle(bigIcon)
                Else
                    Return Icon.FromHandle(smallIcon)
                End If
            Else
                Return Nothing
            End If
    Here it creates an icon from handle with the bigIcon integer or the smallIcon integer. The Icon created is returned as the function's value.



    Then end the function:

    Code:
        End Function




    And there we go, here's an example how to use it, observe that on error will occur if the function returns nothing:

    Code:
        Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Me.BackgroundImage = ReturnIcon("C:\Program Files\Internet Explorer\iexplore.exe", 0).ToBitmap
        End Sub
    Here I just add the icon to the background of the mainform just for test. Remember if you want to use the icons in a picturebox or something like that add .ToBitmap at the end as shown above.





    I'm sure you can find this usefully

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Extracting Icon from .exe

    Very cool tutorial! +rep.

  4. #3
    Join Date
    Oct 2008
    Location
    Istog, Kosova
    Posts
    4,001
    Blog Entries
    1
    Rep Power
    40

    Re: Extracting Icon from .exe

    +rep
    Interested in participating in community events?
    Want to harness your programming skill and turn it into absolute prowess?
    Come join our programming events!

  5. #4
    Ronin is offline Programming Professional
    Join Date
    Apr 2006
    Posts
    309
    Rep Power
    24

    Re: Extracting Icon from .exe

    Would this be hard to convert to C#?

  6. #5
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Re: Extracting Icon from .exe

    I don't know how to convert this:

    Code:
    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
    But the rest would look like this:




    Code:
    1. private Icon ReturnIcon(string Path, int Index, bool small)
    2. {
    3. int bigIcon = 0;
    4. int smallIcon = 0;
    5. ExtractIcon(Path, Index, bigIcon, smallIcon, 1);
    6. if (bigIcon == 0) {
    7. ExtractIcon(Path, 0, bigIcon, smallIcon, 1);
    8. }
    9. if (bigIcon != 0) {
    10. if (small == false) {
    11. return Icon.FromHandle(bigIcon);
    12. }
    13. else {
    14. return Icon.FromHandle(smallIcon);
    15. }
    16. }
    17. else {
    18. return null;
    19. }
    20. }

  7. #6
    Tuve2 is offline Newbie
    Join Date
    Aug 2009
    Posts
    23
    Rep Power
    0

    Re: Extracting Icon from .exe

    nice tutorial!

  8. #7
    Join Date
    Nov 2008
    Location
    Kosovo.
    Posts
    2,391
    Rep Power
    30

    Re: Extracting Icon from .exe

    Very Neat Tutorial .

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. extracting info
    By boosali in forum C and C++
    Replies: 9
    Last Post: 11-24-2009, 11:49 AM
  2. Extracting icons from exe, ico and dll files
    By ThemePark in forum C and C++
    Replies: 10
    Last Post: 07-15-2009, 04:21 PM
  3. Extracting parts of a web page...
    By Jurgnetje in forum PHP Development
    Replies: 2
    Last Post: 07-06-2009, 05:59 PM
  4. [ask] Extracting pdf 's metadata
    By summersgone in forum Java Help
    Replies: 2
    Last Post: 04-05-2008, 09:19 AM
  5. Extracting singel digits from an int.
    By Christine in forum C and C++
    Replies: 2
    Last Post: 01-18-2008, 09:23 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts