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