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.
This is what we use to get the handle from the icons so we can create the icons.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
Create a new function:
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.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
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:
...try to get the icon at index 0.Code:If bigIcon = 0 Then ExtractIcon(Path, 0, bigIcon, smallIcon, 1) End If
If one of the above thing worked we can use one of the integers to get the icon we want, else return nothing:
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.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
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:
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.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
I'm sure you can find this usefully![]()
Very cool tutorial! +rep.
+rep![]()
Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!
Would this be hard to convert to C#?
I don't know how to convert this:
But the rest would look like 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
Code:
- 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;
- }
- }
nice tutorial!
Very Neat Tutorial .
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks