views:

3963

answers:

6

Windows explorer has the ability to show thumbnails of files. These thumbnails are provided by core and third-party shell extensions.

I know how to extend the shell to provide thumbnails to Windows.

What I want to do is retrieve the thumbnail image from any file on the system via the shell using C#. Is this possible?

Essentially, I'm writing a custom file browser and I want to show thumbnails, and can't possibly parse every file on the planet to make my own thumbnails.

Clarification: Many answers seem to be centered around web page thumbnails, or scaling an image. But that's not at all what I'm looking for. What I want is to ask Windows for the thumbnail representation of these file types: .DOC, .PDF, .3DM, .DWG... and mabye about a dozen more. I don't want to parse, render, and make thumbnails myself, because Windows already knows how.

The code I posted as an answer actually works... maybe it can be simplified and cleaned up a bit.

A: 

Not exactly what you asked, but here's a project to open a Thumbs.db file.
The Wikipedia article has more information on where to find thumbnails in Vista & 7.

hemisphire
Nope, you're right - it's not what I'm looking for :)
Brian Gillespie
+1  A: 
Brian Gillespie
to view an EE page copy the url, go to google, search for it, click the result, scroll to the bottom and there are the answers. They should be banned from google....
corymathews
If you have the Google Toolbar installed right click on the page in the domain in question and select Page Info -> Cached Snapshot of Page to see what they served Google when it crawled there.
Matthew Lock
This is a **VERY** complicated way of using GDI+ in C#, for a C# wrapper for GDI+ comes within the DotNet framework.
Eran Betzalel
I believe _Thumbnail Extraction Using the Shell_ at http://www.vbaccelerator.com/home/net/code/libraries/Shell_Projects/Thumbnail_Extraction/article.asp is the original article the code is from. I thought the code looked familar from the unusual way the Enum's were named, such as EIEIFLAG. It dates from April 2003.
AMissico
Here is a clean simplier version from July 2005. _Microsoft Office Thumbnails in SharePoint_ at http://msdn.microsoft.com/en-us/library/aa289172(VS.71).aspx. This code and the article's code share similarities. (It looks like the _Thumbnail Extraction Using the Shell_ article is the basis for the SharePoint article.)
AMissico
I have done some basic testing between the different implementations. Right now, they both work. Yet, the parameter longestEdge of GetThumbnailImage in July 2005 articles is ignored and thumbnail is hard-coded to 500. The flags passed to GetLocation are also different between the two. More to come.
AMissico
I am positive _Thumbnail Extraction Using the Shell_ at vbaccelerator.com/home/net/… is the original article and source code. (See ThumbnailCreator.cs.)
AMissico
+4  A: 

This may help:

http://blogs.msdn.com/windowssdk/archive/2009/06/12/windows-api-code-pack-for-microsoft-net-framework.aspx

This includes the Shell API, and allows you to do almost everything that the shell can (including rendering thumbnails).

John Gietzen
A: 

You should use the windows GDI+ to get these thumbnails, like this.

Eran Betzalel
While that's a short sweet answer, it doesn't do what I need. It only works for .jpg, .gif, .png, .bmp and whatever other files are supported in .NET.If you read through the code you called complex, you'll find that it's actually calling into the IExtractImage method in some COM object with the GUID {BB2E617C-0920-11d1-9A0B-00C04FC2D6C1}. This is much more involved than just scaling an image.
Brian Gillespie
The DotNet component I presented use GDI+ that is directly from Win32API, which means that this component supports everything that the OS supports. If the number of file types that the OS supports does not suit your needs, you should look for a 3rd party module to handle your images.
Eran Betzalel
+3  A: 

Ran across this today -- it's a few months old, but it got the job done for me (on Win7, extracting thumbnails on MPEG-4 files):

http://blogs.msdn.com/windowssdk/archive/2009/06/12/windows-api-code-pack-for-microsoft-net-framework.aspx

ShellFile shellFile = ShellFile.FromFilePath(pathToYourFile);
Bitmap shellThumb = shellFile.Thumbnail.ExtraLargeBitmap;

Hope it helps!

Christian Nunciato
Awesom, thanks heaps... was looking at the code above thinking geez, does it really have to be that hard.Worked a treat! +1
Lightweight
+6  A: 
AMissico
AMissico - thanks for this. I haven't tested your code, but your reasoning seems sound, therefore I'll accept your answer as the correct one rather than the one I found on EE.
Brian Gillespie
Thanks for the sample project, AMissico. You should consider putting it on CodePlex or GoogleCode, so others can inspect it and submit improvements.
dthrasher
Also, I've tested your project against the other code on vbaccelerator. Yours performs slightly faster. (I'll take your word on the memory issues.)
dthrasher
The performance increase is probably related to the codes lack of enumerating over the shell folder's objects. The code is essentially the same.
AMissico
@dthrasher, I notice the other day from the ftp logs that the project is getting downloaded a few times a week. After I finish a pet project, I plan on creating a test project and publishing the code to CodeProject or CodePlex.
AMissico