Welcome to MSDN Blogs Sign in | Join | Help

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

 

 

Published Tuesday, November 06, 2007 5:20 PM by Calvin_Hsia
Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# MSDN Blog Postings &raquo; Extract TreeView or ListView ImageList icons from a DLL

# re: Extract TreeView or ListView ImageList icons from a DLL

Wednesday, November 07, 2007 8:33 AM by Carlos Alloatti

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

# re: Extract TreeView or ListView ImageList icons from a DLL

Wednesday, November 07, 2007 5:32 PM by Jerry Ockenfels

I agree the VFP version would be nice to see.

# re: Extract TreeView or ListView ImageList icons from a DLL

Thursday, November 08, 2007 7:28 AM by Cesar Chalom

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

# Extract Icons from a DLL with GdiPlusX

Sunday, November 11, 2007 9:37 PM by Cesar Chalom

This week Calvin Hsia came with a very interesting post, Extract TreeView or ListView ImageList...

# Extract Icons from a DLL with GdiPlusX

Sunday, November 11, 2007 9:37 PM by Cesar Chalom

This week Calvin Hsia came with a very interesting post, Extract TreeView or ListView ImageList...

# Extract Icons from resources in a DLL with GdiPlusX

Tuesday, November 13, 2007 7:17 AM by Cesar Chalom

This week Calvin Hsia came with a very interesting post, Extract TreeView or ListView ImageList...

# Extract Icons from resources in a DLL with GdiPlusX

Sunday, May 25, 2008 10:25 PM by VFP IMAGING

This week Calvin Hsia came with a very interesting post, Extract TreeView or ListView ImageList...

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker