thumbnail of file (like explorer does)

  • Thread starter Thread starter Brian Henry
  • Start date Start date
B

Brian Henry

Is there anyway to get a thumbnail of a file similar to the way explorer
does that can be used in an application? I want to for example if i drag a
file over a picture box, if it has the ability to have a thumbnail, show it
in the picture box.

thanks!
 
Hi,

You will have to P/Invoke , look into the archives of
microsoft.public.dotnet.languages.csharp this was answer last week along
with a link to MSDN
take a look at www.pinvoke.net you will find there the signatures you need.

pd:
It's not a good thing to crosspost though.

cheers,
 
Brian said:
Is there anyway to get a thumbnail of a file similar to the way explorer
does that can be used in an application? I want to for example if i drag a
file over a picture box, if it has the ability to have a thumbnail, show it
in the picture box.

thanks!

Some code I ran into on the web while looking for something just like
this:

// Please do not remove :)
// Written by Kourosh Derakshan
//
using System.IO;
using System.Drawing.Imaging;

private const int THUMBNAIL_DATA = 0x501B;


/// <summary>
/// Gets the thumbnail from the image metadata. Returns null of no
thumbnail
/// is stored in the image metadata
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
Image GetThumbnail (string path)
{
FileStream fs = File.OpenRead (path);
// Last parameter tells GDI+ not the load the actual image data
Image img = Image.FromStream (fs, false, false);


// GDI+ throws an error if we try to read a property when the image
// doesn't have that property. Check to make sure the thumbnail
property
// item exists.
bool propertyFound = false;
for (int i=0; i<img.PropertyIdList.Length; i++)
if (img.PropertyIdList == THUMBNAIL_DATA)
{
propertyFound = true;
break;
}

if (!propertyFound)
return null;

PropertyItem p = img.GetPropertyItem (THUMBNAIL_DATA);
fs.Close();
img.Dispose();


// The image data is in the form of a byte array. Write all
// the bytes to a stream and create a new image from that stream
byte[] imageBytes = p.Value;
MemoryStream stream = new MemoryStream (imageBytes.Length);
stream.Write (imageBytes, 0, imageBytes.Length);

return Image.FromStream(stream);
}

A big thanks to Kourosh.

Matt
 
You need to use the shells IExtractImage interface.


This will show you how...
http://www.vbaccelerator.com/home/NET/Code/Libraries/Shell_Projects/Thumbnail_Extraction/article.asp


--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
well that works the best so far, I can get it to give me a thumb of a DOC
and Visio file which I need, but I also need PDF thumbs (Acrobat 7 does
thumbs now) and when i select a pdf with that test app it says "Failed to
get thumbnail: The data necessary to complete this operation is not yet
available."
 
Are the thumbnails visible in the explorer window? If they are they should
be extractable by this method. If the thumbnails are just specific to the
file open dialog in acrobat I would suspect a custom implementation rather
than a shell integration solution.

--
Bob Powell [MVP]
Visual C#, System.Drawing

Ramuseco Limited .NET consulting
http://www.ramuseco.com

Find great Windows Forms articles in Windows Forms Tips and Tricks
http://www.bobpowell.net/tipstricks.htm

Answer those GDI+ questions with the GDI+ FAQ
http://www.bobpowell.net/faqmain.htm

All new articles provide code in C# and VB.NET.
Subscribe to the RSS feeds provided and never miss a new article.
 
Back
Top