Results 1 to 4 of 4

Thread: [RESOLVED] How to get the image shown by Explorer when viewing by Medium/Large/Extra Large Icons

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2011
    Location
    Palm Coast, FL
    Posts
    760

    Resolved [RESOLVED] How to get the image shown by Explorer when viewing by Medium/Large/Extra Large Icons

    In Windows File Explorer, browsing a folder containing image files, when I select to View by Small Icon, a generic file type icon is shown. When I select to View by Medium/Large/Extra Large Icon, a thumbnail of the image is shown.

    I know how to programmatically get the file type icon image from the system image list. How do I get the medium/large/extra large thumbnail of the image and load that into an image list for use in my program (listview, dialogs, etc...)

    Thanks in advance for any guidance you can provide!

  2. #2
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,653

    Re: How to get the image shown by Explorer when viewing by Medium/Large/Extra Large I

    To get the best results like you see in Explorer, you need to use a combination of methods to handle every scenario: [VB6, Vista+] Advanced Thumbnail ListView: Icons, images, videos, framed small images


    The main function of interest though, if you just want limited support, is the IShellItemImageFactory class:

    Code:
    Public Declare Function SHCreateItemFromIDList Lib "shell32" (ByVal pidl As Long, riid As UUID, ppv As Any) As Long
    Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Public Declare Function ILCreateFromPathW Lib "shell32" (ByVal pwszPath As Long) As Long
    Public Declare Sub CoTaskMemFree Lib "ole32.dll" (ByVal PV As Long) ' Frees memory allocated by the shell
    
    Public Function FileToHIML(himl As Long, pidlFQ As Long, CX As Long, CY As Long, lFlags As SIIGBF) As Long
    Dim isiif As IShellItemImageFactory
    Dim hr As Long
    Dim hBmp As Long
    On Error GoTo e0
    
    hr = SHCreateItemFromIDList(pidlFQ, IID_IShellItemImageFactory, isiif)
    hr = isiif.GetImage(CX, CY, lFlags, hBmp)
    If hr = S_OK Then
        FileToHIML = ImageList_Add(himl, hBmp, 0)
        DeleteObject hBmp
    Else
        FileToHIML = -1
    End If
    
    Set isiif = Nothing
    
    Exit Function
    e0:
    Debug.Print "FileToHIML Error: " & Err.Description & ", hr=0x" & Hex$(hr)
    FileToHIML = -1
    End Function
    which is called like
    Code:
        pidl = ILCreateFromPathW(StrPtr(App.Path & "\Images\32.png"))
        hr = FileToHIML(himl, pidl, cxThumb, cyThumb, SIIGBF_THUMBNAILONLY)
        Call CoTaskMemFree(pidl)
    where himl is the handle to your imagelist (API imagelist; if you're not using that I can provide more details),

    That will get a thumbnail if it has one (and the image file isn't smaller than the requested size; that's one of the exceptions the advanced thumbview handles).
    If that fails (hr=-1), re-request it with SIIGBF_ICONONLY.

    That covers all files; you still get the default icon for tiny images and any non-image file (the full size icon too; it will load 256x256 icons if requested).

    You might also want to look at my new project, [VB6] Dynamic Resize: Use a slider to change ListView icon/thumbnail size on the fly
    Last edited by fafalone; Aug 19th, 2017 at 07:52 PM.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2011
    Location
    Palm Coast, FL
    Posts
    760

    Re: How to get the image shown by Explorer when viewing by Medium/Large/Extra Large I

    Exactly what I was looking for! Well, not EXACTLY. I was hoping Windows provided one nice neat API method which handled all of the various cases. But in the absence of that I do appreciate all of your and dilettante's efforts to put this together. I downloaded and ran your "Advanced Thumbnail ListView" sample project and it works great. Thank you!

  4. #4
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    7,653

    Re: [RESOLVED] How to get the image shown by Explorer when viewing by Medium/Large/Ex

    If you just want to mimic the current Explorer settings for normal icon view (as opposed to Explorer's Thumbnail View), you can just use the FileToHIML function alone with SIIGBF_ICONONLY; that will show the icon Explorer does even if it's a thumbnail, as long as it's a thumbnail in normal icon view, and the image isn't smaller than the requested size. You don't have to worry about fallbacks for that as at worst it will return an icon. Note that if the user has thumbnails disabled, they'll be disabled in your program too then. I've also revised the function above to have an error handler like it should.
    Last edited by fafalone; Aug 19th, 2017 at 07:50 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width