Extract the Associated Icon from a File Easily using VB 2008/2010

Microsoft added a new feature in Visual Basic 2005 (and is present in 08 as well) that will very easily get the icon associated with a specified file. In the past you would have to use the Windows APIs to extract a icon from a file. But now you can use a feature in Version 2.0 and higher of the DotNET Framework to perform this task which is what this little tip will show.

The function to use is ‘ExtractAssociatedIcon’ that is located under the System.Drawing.Icon namespace.

Public Shared Function ExtractAssociatedIcon(ByVal filePath As String) As System.Drawing.Icon

As you can see this is a very simple function. All you need to do is specify the path of the file whose icon you want to extract and handle the return value of the function which is the Icon associated with the file specified in the filePath parameter.

The code below will get the Icon associated with the file I specified and display the Icon in a Picturebox control.

picSource.Image = Drawing.Icon.ExtractAssociatedIcon(“pathAndFileToGetIcon”).ToBitmap()

Using the code above, i’m using a picturebox that I named picSource while converting the Icon to a Bitmap which can then be displayed easier in the picturebox image property. IF you want to save the icon image displayed in the picturebox control, the small piece of code below will do the trick.


Well, thats all there is to it I guess. Since MS added this ‘ExtractAssociatedIcon’ Function to the framework you no longer have to use the APIs to get similar results if your using Visual Basic .NET 2005 or version 2008. Have Fun!


6 thoughts on “Extract the Associated Icon from a File Easily using VB 2008/2010

  1. Martin

    Hi Jason – just be careful, because extracted icons are extremely ugly 🙁 I dont know if they are 8 bit or if it is color depth (need to check), but they appear to be much uglier than original icons 🙁


  2. Jason Post author

    Hi, VB6 does not have the required class. You can access the class by interoperating/fusion but it probably wouldn’t be worth the time to do. Just use the Windows API.

    Private Declare Function ExtractIcon Lib “shell32.dll” Alias “ExtractIconA” (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long

    Private Declare Function ExtractIconEx Lib “shell32.dll” Alias “ExtractIconExA” (ByVal lpszFile As String, ByVal nIconIndex As Long, phiconLarge As Long, phiconSmall As Long, ByVal nIcons As Long) As Long

    Either of those APIs should work for you. 🙂


  3. Greg

    I am using VB6, and i do not get the same icon that appears on the desktop for some the shortcuts. I am trying to get the icon exactly what appears on the desktop. It seems that the icon for those shortcuts have been manually set to show a “.ico” file. I cannot find that reference anywhere. I think I need to find where the PROPERTIES for that desktop entry are located. Anyone have any experience with that?


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.