Jump to content

Extracting Icon from .exe

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
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.

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:

    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:


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:

        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:

        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:

    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:

    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 :D

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Very cool tutorial! +rep.

#3
MathX

MathX

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,001 posts
+rep :)

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


#4
Ronin

Ronin

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 309 posts
Would this be hard to convert to C#?

#5
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
I don't know how to convert this:

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:




 [LIST=1]
[*]private Icon ReturnIcon(string Path, int Index, bool small)
[*]{
[*]   int bigIcon = 0;
[*]   int smallIcon = 0;
[*]   ExtractIcon(Path, Index, bigIcon, smallIcon, 1);
[*]   if (bigIcon == 0) {
[*]       ExtractIcon(Path, 0, bigIcon, smallIcon, 1);
[*]   }
[*]   if (bigIcon != 0) {
[*]       if (small == false) {
[*]           return Icon.FromHandle(bigIcon);
[*]       }
[*]       else {
[*]           return Icon.FromHandle(smallIcon);
[*]       }
[*]   }
[*]   else {
[*]       return null;
[*]   }
[*]}
[/LIST]


#6
Tuve2

Tuve2

    Newbie

  • Members
  • PipPip
  • 23 posts
nice tutorial!

#7
mendim.

mendim.

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,393 posts
Very Neat Tutorial .