You are on page 1of 5

using using using using using using using using using using

Microsoft.VisualBasic; System; System.Collections; System.Collections.Generic; System.Data; System.Diagnostics; System.IO; System.Drawing; system.Runtime.InteropServices; System.Text;

internal class Thumbnails { private static readonly Guid IID_ISHELLFOLDER = new Guid("000214E6-00000000-C000-000000000046"); private static readonly Guid IID_IEXTRACTIMAGE = new Guid("BB2E617C-0920 -11d1-9A0B-00C04FC2D6C1"); public static Bitmap ExtractThumbNail(FileInfo file) { return ExtractThumbNail(file, new System.Drawing.Size(100, 100)) ; } public static Bitmap ExtractThumbNail(FileInfo file, Size imageSize) { Bitmap thumbnail = null; //Dim alloc As IMalloc = Nothing IShellFolder folder = null; IShellFolder item = null; IntPtr pidlFolder = IntPtr.Zero; IntPtr hBmp = IntPtr.Zero; IExtractImage extractImage = null; IntPtr pidl = IntPtr.Zero; if ((file.Exists)) { try { SHGetDesktopFolder(ref folder); if ((folder != null)) { int cParsed = 0; int pdwAttrib = 0; int HR = folder.ParseDisplayName(IntPtr. Zero, IntPtr.Zero, file.Directory.FullName, ref cParsed, ref pidlFolder, ref pdw Attrib); if (HR < S_OK) return null; if (!pidlFolder.Equals(IntPtr.Zero)) { HR = folder.BindToObject(pidlFol der, IntPtr.Zero, ref IID_ISHELLFOLDER, ref item); if (HR < S_OK) return null;

Equals(IntPtr. ref IID_IEXTRACTIMAGE. ref pidl.Equals (IntPtr. } } } } } } finally { if (!hBmp.IEIFLAG_ORIGSIZE | IEIFLAG. if (!hBmp.Zero. 1. ref uFlags). } if ((item != null)) { Marshal.GetUIObjectOf( 0.FreeCoTaskMem(pidlFolder).Zero)) { thumbnai l = Bitmap. extractImage = null. HR = extractImag e.ReleaseComObject(extractImage). if ((extractImage != nul l)) { StringBuilder lo cation = new StringBuilder(MAX_PATH.Equals(IntPtr. if (HR < S_OK) return n ull.ThrowExceptionFo rHR(HR). HR = extractImag e. Marshal. location.Capacity.IEIFLAG_QUALITY. ref extractImage). IntPtr. if (HR < S_OK) return null. ref 0.Zero.IEIFLAG_ASPECT | IEIFLAG. int uFlags = IEI FLAG. int requestedCol orDepth = 32. if (HR < S_OK) return n ull. file. MAX_PATH).ParseDisplayNa me(IntPtr.Name.if ((item != null)) { HR = item.Extract(ref hBmp).Zero)) DeleteObject(hBmp). . if (!pidlFolder. ref 0).ReleaseComObject(item). ref imageSize. } if ((extractImage != null)) { Marshal. int prgf = 0. requeste dColorDepth.GetLocation(location. int priority = 0 . HR = item. new IntPtr[] { pidl }. ref prgf.Zero)) { Marshal. ref priority.FromHbitmap(hBmp).

CharSet = CharSet. [DllImport("gdi32". GuidAttribute("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1").Auto. ref IntPtr ppenumIDList). CharSet = CharSet. [PreserveSig()] int BindToObject(IntPtr pidl. MarshalAs(UnmanagedType. In terfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)] internal interface IExtractImage { [PreserveSig()] int GetLocation( [Out().Auto)] private static extern int DeleteObject(IntPtr hObject). } [ComImportAttribute(). IntPtr pbcReserved. [Marshal As(UnmanagedType.InterfaceIsIUnkno wn). folder = null. public static readonly Guid IID_ContextMenu = new Guid("000214e4-0000-00 00-c000-000000000046"). IntPtr pbcReserved. ref IntPtr ppidl. InterfaceTypeAttribute(ComInterfaceType. ref int pchEaten.LPWStr)] StringBuilder pszPathBuffer. int dwRecClrDepth. private const int MAX_PATH = 260. } [ComImportAttribute().item = null. [PreserveSig()] int Extract( [Out()] ref IntPtr phBmpThumbnail). Guid("000214E6-0000-0000-C000-000000000046")] internal interface IShellFolder { [PreserveSig()] int ParseDisplayName(IntPtr hwndOwner. } } } return thumbnail. ref Guid riid. ref int pdwPriority. //IShellFolder) As Integer . int cch. [DllImport("shell32". Exact Spelling = true)] private static extern int SHGetDesktopFolder(ref IShellFolder ppshf). ref int pdwFlags). } private const int S_OK = 0. ref ISh ellFolder ppvOut). SetLastError = true. } if ((folder != null)) { Marshal.ReleaseComObject(folder).LPWStr)] string lpszDisplayName.U4)] int grfFlags. [MarshalAs(UnmanagedType. ref Size prgSize. ref int pdwAttribute s). [PreserveSig()] int EnumObjects(int hwndOwner.

ref Guid riid. ref Guid riid.[PreserveSig()] int BindToStorage(IntPtr pidl.LPWStr)] string lpszName. // returned from the extractor if it does NOT want an icon stamp on the . //IUnknown) As Integer [PreserveSig()] int GetAttributesOf(int cidl. // render as if for the screen (this is exlusive with IEIFLAG_ASPECT ) IEIFLAG_ORIGSIZE = 0x40. //returned from the extractor if it does NOT cache the thumbnail IEIFLAG_ASPECT = 0x4. } [MarshalAs(UnmanagedType [Flags()] internal enum IEIFLAG { IEIFLAG_ASYNC = 0x1.IUnknown)] ref object ppvOut). IntPtr pbcReserved. // passed to the extractor to beg it to render to the aspect ratio of th e supplied rect IEIFLAG_OFFLINE = 0x8. [Out().LPArray. [MarshalAs(UnmanagedType. [PreserveSig()] int CompareIDs(IntPtr lParam. IntPtr pidl. ref IntPtr ppidlOut). ramindex = 0)] IntPtr[] apidl. int cidl. //ByRef ppvOut As IUnknown) As Integer //ByRef ppvOut As IDropTarget) As Integer [PreserveSig()] int GetDisplayNameOf(IntPtr pidl.U4)] [PreserveSig()] int SetNameOf(int hwndOwner. [MarshalAs(UnmanagedType. [Out()] ref int prgfInOut. . ref IntPtr ppvOut) . IntPtr pidl1. ref int rgfInOut). ref Guid riid. IntPtr pidl2). sizeparamindex = 0)] IntPtr[] apidl. IntPtr lpName). // ask the extractor if it supports ASYNC extract (free threaded) IEIFLAG_CACHE = 0x2. // if the extractor shouldn't hit the net to get any content needed for the rendering IEIFLAG_GLEAM = 0x10. sizepa [PreserveSig()] int GetUIObjectOf(int hwndOwner. [MarshalAs(UnmanagedType. IntPtr ppvObj). but crop if neccessary IEIFLAG_NOSTAMP = 0x80. //does the image have a gleam ? this will be returned if it does IEIFLAG_SCREEN = 0x20. [MarshalAs(UnmanagedType .LPArray. MarshalAs(UnmanagedType. // render to the approx size passed. [PreserveSig()] int CreateViewObject(IntPtr hwndOwner.U4)] int uFlags. int uFlags.

higher qualit y image is desired. //returned from the extractor if it does NOT want an a border around the thumbnail IEIFLAG_QUALITY = 0x200 // passed to the Extract method to indicate that a slower. re-compute the thumbnail } .thumbnail IEIFLAG_NOBORDER = 0x100.