15

I'm trying to get a thumbnail of a file in Windows API. I did find a way, but the code example requires Windows version 8 minimum, I would prefer a way that works in at least 7 or Vista, preferably in XP. If there is a platform independent way of obtaining the thumbnail I would prefer to do that, but I haven't been able to find one.

6

2 Answers 2

22

There is no single API that works on all Windows versions, because Microsoft keeps changing the thumbnail APIs from one Windows version to another.

On Win2K up to and including Vista (not sure about later versions), you can retrieve an IShellFolder for the file's parent folder using SHGetDesktopFolder() and IShellFolder::ParseDisplayName() (or SHParseDisplayName() on XP and later), then use IShellFolder::GetUIObjectOf() to retrieve the desired child file's IExtractImage interface, and then call its GetLocation() method to set the image size and its Extract() method to retrieve the actual image.

On Vista and later, you can either:

1) use IThumbnailProvider. Query it for one of its IInitializeWith... interfaces (IInitializeWithStream, IInitializeWithItem, or IInitializeWithFile) to tell it which file you are interested in, and then call its GetThumbnail() method to get the actual image. Alternatively, you can get an IShellItem for the desired file and then call its BindToHandler method to obtain the file's IThumbnailProvider.

2) use IThumbnailCache. Pass an IShellItem representing the desired file to its GetThumbnail() method to get the image.

3) use IShellItemImageFactory. Use one of the SHCreateItemFrom...() functions (SHCreateItemFromIDList(), SHCreateItemFromParsingName(), SHCreateItemFromRelativeName(), SHCreateItemInKnownFolder(), SHCreateItemWithParent()) to obtain this interface for a given file, then call its GetImage() method.

1

Using on a Win 7 system:

PS> $myimage = [System.Drawing.Image]::FromFile("c:\image.jpg");
PS> $mythumb = $myimage.GetThumbnailImage(72, 72, $null, [intptr]::Zero);
PS> $mythumb.Save("c:\thumb.jpg")
3
  • 1
    This won't do since it gets the entire image, the point with a thumbnail cache is mostly performance.
    – Dude Dawg
    Oct 22, 2013 at 17:15
  • @DudeDawg According to this, it will get the thumbnail if it exists: "If the Image contains an embedded thumbnail image, this method retrieves the embedded thumbnail and scales it to the requested size. If the Image does not contain an embedded thumbnail image, this method creates a thumbnail image by scaling the main image."
    – David
    Oct 22, 2013 at 17:21
  • @David: That's for thumbnails which are embedded in the System.Drawing.Image .Net class. Your random image.jpg probably does not contain one, and FromFile doesn't try the IThumbnailCache.
    – MSalters
    Apr 4, 2022 at 16:04

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.