public System.Drawing.Bitmap GetThumbnail(string file, int width, int height) { if ((!File.Exists(file)) && (!Directory.Exists(file))) { throw new FileNotFoundException( String.Format("The file '{0}' does not exist", file), file); } if (thumbNail != null) { thumbNail.Dispose(); thumbNail = null; } IShellFolder folder = null; try { folder = GetDesktopFolder; } catch (Exception ex) { throw ex; } if (folder != null) { IntPtr pidlMain = IntPtr.Zero; try { int cParsed = 0; int pdwAttrib = 0; string filePath = Path.GetDirectoryName(file); pidlMain = IntPtr.Zero; folder.ParseDisplayName( IntPtr.Zero, IntPtr.Zero, filePath, out cParsed, out pidlMain, out pdwAttrib); } catch (Exception ex) { Marshal.ReleaseComObject(folder); throw ex; } if (pidlMain != IntPtr.Zero) { // IShellFolder: Guid iidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046"); IShellFolder item = null; try { folder.BindToObject(pidlMain, IntPtr.Zero, ref iidShellFolder, ref item); } catch (Exception ex) { Marshal.ReleaseComObject(folder); UnmanagedMethods.CoTaskMemFree(pidlMain); throw ex; } if (item != null) { IEnumIDList idEnum = null; try { item.EnumObjects( IntPtr.Zero, (ESHCONTF.SHCONTF_FOLDERS | ESHCONTF.SHCONTF_NONFOLDERS), ref idEnum); } catch (Exception ex) { Marshal.ReleaseComObject(folder); UnmanagedMethods.CoTaskMemFree(pidlMain); throw ex; } if (idEnum != null) { int hRes = 0; IntPtr pidl = IntPtr.Zero; int fetched = 0; bool complete = false; while (!complete) { hRes = idEnum.Next(1, ref pidl, out fetched); if (hRes != 0) { pidl = IntPtr.Zero; complete = true; } else { if (GetThumbnail(file, pidl, item, width, height)) { complete = true; } } if (pidl != IntPtr.Zero) { UnmanagedMethods.CoTaskMemFree(pidl); } } Marshal.ReleaseComObject(idEnum); } Marshal.ReleaseComObject(item); } UnmanagedMethods.CoTaskMemFree(pidlMain); } Marshal.ReleaseComObject(folder); } return(thumbNail); }