I have made an application that presents you a list of files in your computer. Whenever you click any item in the list, a small PictureBox next to it should show the thumbnail of the corresponding file. I am using C# on Windows 7.
To obtain the thumbnail, I've recurred to a method posted in a different question. First, I reference the Windows API Code pack. Then, I use the following code:
ShellFile shellFile = ShellFile.FromFilePath(fullPathToFile);
myPictureBox.Image = shellFile.Thumbnail.LargeBitmap;
This doesn't always work. Sometimes, the thumbnail shown is merely the 'default application' icon. I've found out that the real thumbnail is only shown if Windows had previously generated the thumbnail for that file and stored it in the thumbnails cache. This means that I have to manually open a folder, wait for Windows to generate thumbnails for each file, and then my application will be able to see those thumbs.
How can my program force Windows 7 to generate real thumbnails before using them?
Update (by Li0liQ)
It is possible to force thumbnail retrieval by adding FormatOption:
ShellFile shellFile = ShellFile.FromFilePath(fullPathToFile);
shellFile.Thumbnail.FormatOption = ShellThumbnailFormatOption.ThumbnailOnly;
myPictureBox.Image = shellFile.Thumbnail.LargeBitmap;
however, I'm getting an exception in case thumbnail is not there yet:
The current ShellObject does not have a valid thumbnail handler or there was a problem in extracting the thumbnail for this specific shell object. ---> System.Runtime.InteropServices.COMException: Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG))
See How do I refresh a file's thumbnail in Windows Explorer? question and code snippet for potential clues.
After some searching, I found Microsoft Office Thumbnails in Sharepoint. It might only work on office documents, but might be what you want. Will require some translation as the examples are in C++ and VB.Net.
How can my program force Windows 7 to generate real thumbnails before using them?
From Shell Interfaces: IThumbnailCache interface (emphasis mine)
So, assuming your system has an implementation of IThumbnail provider that works with PDF:
thumbNailCache
.thumbNailCache.GetThumbnail
(shellItem, thumbNailpx,
wtxFlagSet
, out thumbNailBitmap);
. The documentation says this will create the thumbnail for you and return it in the out parameter thumbNailBitmap.Caveats: I don't know if your Code Pack fully exposes IThumbnailCache, but if it does, this looks to be pretty straight forward. If it doesn't you'll have to import it from shell32.dll.
There's a IThumbnailProvider interface as well that may work for you, but read the community comments. It seems to be fragile and tricky to use.
Here is a piece of code that extracts thumbnail bitmaps (using Windows Vista or higher only). It's based on the cool IShellItemImageFactory interface:
Additional notes:
GetImage
method has various interesting flags (SIIGBF
) you can play with.REGDB_E_CLASSNOTREG
is a typical kind of error you may get in these cases.As far as I know you cannot force Windows 7 to generate thumbnails. However, you can create it with the code by yourself:
http://msdn.microsoft.com/en-us/library/system.drawing.image.getthumbnailimage.aspx