Partager via


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

    Const LOAD_LIBRARY_AS_IMAGE_RESOURCE = &H20

    Const LOAD_LIBRARY_AS_DATAFILE = 2

    <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

        FreeLibrary(hModule)

        Me.Controls.Add(lv)

        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)

        imlist.Images.AddStrip(bmpIcons)

        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

        Next

    End Sub

End Class

Comments

  • Anonymous
    November 06, 2007
    PingBack from http://msdnrss.thecoderblogs.com/2007/11/06/extract-treeview-or-listview-imagelist-icons-from-a-dll/

  • Anonymous
    November 07, 2007
    Calvin: Thank you for your post, it would be nice if we could have a VFP version of the code.

  • Anonymous
    November 07, 2007
    I agree the VFP version would be nice to see.

  • Anonymous
    November 07, 2007
    Hi Calvin, Just another point: Isn't it necessary to delete the hBitmap handle manually ? DeleteObject(hMem) ??? I'm not sure if in VB it is necessary or not ... TIA Cesar

  • Anonymous
    November 11, 2007
    This week Calvin Hsia came with a very interesting post, Extract TreeView or ListView ImageList...

  • Anonymous
    November 11, 2007
    This week Calvin Hsia came with a very interesting post, Extract TreeView or ListView ImageList...

  • Anonymous
    November 12, 2007
    This week Calvin Hsia came with a very interesting post, Extract TreeView or ListView ImageList...

  • Anonymous
    May 25, 2008
    This week Calvin Hsia came with a very interesting post, Extract TreeView or ListView ImageList...