Extract TreeView or ListView ImageList icons from a DLL

An ImageList can be used for a ListView or TreeView control to contain many images in a single file, usually embedded as a resource. Visual Studio itself has many icons.

If you open Windows Explorer or Internet Explorer, you can see many icons in toolbars, ListViews or TreeViews.

Using Visual Studio (any version), choose File->Open->File and navigate to C:\Windows\System32\shdocvw.dll. The file will be opened by default in the Resource editor, and you can drill down into the resources embedded in the dll. Drill down into Bitmap and double-click on 328 (xp) or 3215 (Vista). A single bitmap is displayed, made up of many smaller bitmaps placed in a row.

Below is code that will extract individual icons from such resources. It loads a DLL, loads the desired resource, creates a Managed BitMap object and uses it do display the icons in a ListView.

The Bitmap.FromHbitmap(hRes) function will take a resource and convert it into a managed BitMap, which can be used as a ListView and also Saved as an individual file.

You can use GetManifestResourceNames to get resources from a managed assembly.

Start Visual Studio (any version), then choose File->New->Project->Windows Forms Application, then dbl click the form and replace the code with this sample. Then hit F5 to run it.

Imports System.Runtime.InteropServices

Public Class Form1



    <DllImport("kernel32.dll")> _

    Shared Function LoadLibraryEx(ByVal lpFilename As String, ByVal hModule As IntPtr, ByVal dwFlags As UInteger) As IntPtr

    End Function

    <DllImport("user32.dll")> _

    Shared Function LoadBitmap(ByVal hModule As IntPtr, ByVal hResInfo As UInteger) As IntPtr

    End Function

    <DllImport("kernel32.dll")> _

    Shared Function FreeLibrary(ByVal hModule As IntPtr) As IntPtr

    End Function

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Me.Width = 600

       Me.Height = 600

        Dim lv As New ListView

        lv.Height = Me.Width

        lv.Width = Me.Height

        'C:\Windows\System32\shdocvw.dll, 328

        'C:\Windows\System32\browseui.dll 279

        Dim hModule = LoadLibraryEx("C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\msenv.dll", _

                                    0, LOAD_LIBRARY_AS_IMAGE_RESOURCE + LOAD_LIBRARY_AS_DATAFILE)

        Dim hRes = LoadBitmap(hModule, &H4F4)

        Dim bmpIcons = Bitmap.FromHbitmap(hRes)

        bmpIcons.Save("d:\t.bmp") 'optionally save it to inspect it



        lv.View = View.SmallIcon

        Dim imlist As New ImageList

        'imlist.ImageSize = New Drawing.Size(16, 16)

        'imlist.ColorDepth = ColorDepth.Depth24Bit

        imlist.TransparentColor = Color.FromArgb(&HFFFF00FF)


        lv.SmallImageList = imlist

        For i = 0 To 235

            lv.Items.Add(i.ToString, i)

           Dim icon = imlist.Images.Item(i)

            icon.Save("d:\OMBmp" + i.ToString + ".bmp") 'save individual icons


    End Sub

End Class