+ Reply to Thread
Results 1 to 7 of 7

Thread: Extracting Icon from .exe

  1. #1
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    16
    Posts
    8,779
    Blog Entries
    5

    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. #2
    Administrator Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan is a name known to all Jordan's Avatar
    Join Date
    Nov 2005
    Location
    Hendersonville, NC
    Posts
    24,556
    Blog Entries
    97

    Re: Extracting Icon from .exe

    Very cool tutorial! +rep.

  3. #3
    Guru MathX has a spectacular aura about MathX has a spectacular aura about MathX's Avatar
    Join Date
    Oct 2008
    Location
    Kosovo
    Age
    19
    Posts
    4,006

    Re: Extracting Icon from .exe

    +rep

  4. #4
    Programming Professional Ronin is on a distinguished road
    Join Date
    Apr 2006
    Posts
    310

    Re: Extracting Icon from .exe

    Would this be hard to convert to C#?

  5. #5
    Moderator Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe has a reputation beyond repute Vswe's Avatar
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Age
    16
    Posts
    8,779
    Blog Entries
    5

    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. }

  6. #6
    Newbie Tuve2 is an unknown quantity at this point
    Join Date
    Aug 2009
    Posts
    24

    Re: Extracting Icon from .exe

    nice tutorial!

  7. #7
    Guru mendim. is a jewel in the rough mendim. is a jewel in the rough mendim. is a jewel in the rough mendim. is a jewel in the rough mendim.'s Avatar
    Join Date
    Nov 2008
    Location
    Kosovo.
    Posts
    2,393

    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. Replies: 1
    Last Post: 04-26-2009, 07:27 PM
  2. interface for .exe file
    By runjun in forum Software Development Tools
    Replies: 2
    Last Post: 11-20-2008, 03:15 PM
  3. icon help
    By Siten0308 in forum C# Programming
    Replies: 6
    Last Post: 10-29-2008, 04:23 PM
  4. XP Transparent Icon Text
    By TcM in forum Computer Software/OS
    Replies: 2
    Last Post: 01-29-2008, 03:24 AM
  5. Change the default EXE Icon
    By dirkfirst in forum C# Programming
    Replies: 3
    Last Post: 10-03-2006, 06:10 PM

Bookmarks

Bookmarks

     
        Algorithms and Data Structures

        Java tutorials

        Algorithms Forum

Posting Permissions

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