private bool _getThumbNail(string file, IntPtr pidl, IShellFolder item) { IntPtr hBmp = IntPtr.Zero; IExtractImage extractImage = null; IntPtr[] pidl_array = new IntPtr[] { pidl }; try { string pidlPath = PathFromPidl(pidl); if (Path.GetFileName(pidlPath).ToUpper().Equals(Path.GetFileName(file).ToUpper())) { object iunk = null; Guid iidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1"); item.GetUIObjectOf(IntPtr.Zero, 1, pidl_array, ref iidExtractImage, IntPtr.Zero, out iunk); extractImage = (IExtractImage)iunk; if (extractImage != null) { Console.WriteLine("Got an IExtractImage object!"); SIZE sz = new SIZE(); sz.cx = DesiredSize.Width; sz.cy = DesiredSize.Height; StringBuilder location = new StringBuilder(260, 260); int priority = 0; int requestedColourDepth = 32; EIEIFLAG flags = EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_SCREEN; int uFlags = (int)flags; extractImage.GetLocation(location, location.Capacity, ref priority, ref sz, requestedColourDepth, ref uFlags); extractImage.Extract(ref hBmp); if (hBmp != IntPtr.Zero) { _thumbNail = Bitmap.FromHbitmap(hBmp); } Marshal.ReleaseComObject(extractImage); extractImage = null; } return(true); } else { return(false); } } catch (Exception ex) { if (hBmp != IntPtr.Zero) { UnmanagedMethods.DeleteObject(hBmp); } if (extractImage != null) { Marshal.ReleaseComObject(extractImage); } throw ex; } }
private bool GetThumbnail(string file, IntPtr pidl, IShellFolder item, int width, int height) { IntPtr hBmp = IntPtr.Zero; IExtractImage extractImage = null; try { string pidlPath = PathFromPidl(pidl); if (Path.GetFileName(pidlPath).ToUpper().Equals(Path.GetFileName(file).ToUpper())) { IUnknown iunk = null; int prgf = 0; Guid iidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1"); item.GetUIObjectOf(IntPtr.Zero, 1, ref pidl, ref iidExtractImage, out prgf, ref iunk); extractImage = (IExtractImage)iunk; if (extractImage != null) { SIZE sz = new SIZE(); sz.cx = width; sz.cy = height; StringBuilder location = new StringBuilder(260, 260); int priority = 0; int requestedColourDepth = 32; EIEIFLAG flags = EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_SCREEN; int uFlags = (int)flags; extractImage.GetLocation(location, location.Capacity, ref priority, ref sz, requestedColourDepth, ref uFlags); extractImage.Extract(out hBmp); if (hBmp != IntPtr.Zero) { thumbNail = System.Drawing.Bitmap.FromHbitmap(hBmp); } Marshal.ReleaseComObject(extractImage); extractImage = null; } return(true); } else { return(false); } } catch (Exception ex) { if (hBmp != IntPtr.Zero) { UnmanagedMethods.DeleteObject(hBmp); } if (extractImage != null) { Marshal.ReleaseComObject(extractImage); } throw ex; } }
private bool getThumbnail(string file, IntPtr pidl, IShellFolder item) { IntPtr hBmp = IntPtr.Zero; IExtractImage extractImage = null; try { string pidlPath = PathFromPidl(pidl); if (Path.GetFileName(pidlPath).ToUpperInvariant().Equals(Path.GetFileName(file).ToUpperInvariant())) { // we have the item: IUnknown iunk = null; int prgf = 0; Guid iidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1"); item.GetUIObjectOf(IntPtr.Zero, 1, ref pidl, ref iidExtractImage, out prgf, ref iunk); extractImage = (IExtractImage)iunk; if (extractImage != null) { //Got an IExtractImage object! SIZE sz = new SIZE(); sz.cx = desiredSize.Width; sz.cy = desiredSize.Height; StringBuilder location = new StringBuilder(260, 260); int priority = 0; int requestedColourDepth = 32; //EIEIFLAG flags = EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_SCREEN; EIEIFLAG flags = EIEIFLAG.IEIFLAG_ORIGSIZE | EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_QUALITY; int uFlags = (int)flags; // E.g. for PDFs on Vista... try { extractImage.GetLocation(location, location.Capacity, ref priority, ref sz, requestedColourDepth, ref uFlags); } catch (Exception) {} extractImage.Extract(out hBmp); if (hBmp != IntPtr.Zero) { // create the image object: thumbNail = System.Drawing.Bitmap.FromHbitmap(hBmp); // is thumbNail owned by the Bitmap? } Marshal.ReleaseComObject(extractImage); extractImage = null; } return(true); } else { return(false); } } catch (Exception ex) { if (hBmp != IntPtr.Zero) { UnManagedMethods.DeleteObject(hBmp); } if (extractImage != null) { Marshal.ReleaseComObject(extractImage); } throw ex; } }
Bitmap GetThumbnailFromIExtractImage(string fileName, int colorDepth) { IShellFolder desktopFolder; IShellFolder someFolder = null; IExtractImage extract = null; IntPtr pidl; IntPtr filePidl; // Manually define the IIDs for IShellFolder and IExtractImage Guid IID_IShellFolder = new Guid("000214E6-0000-0000-C000-000000000046"); Guid IID_IExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1"); //Divide the file name into a path and file name string folderName = Path.GetDirectoryName(fileName); string shortFileName = Path.GetFileName(fileName); //Get the desktop IShellFolder desktopFolder = getDesktopFolder; //Get the parent folder IShellFolder int cParsed = 0; int pdwAttrib = 0; desktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, folderName, out cParsed, out pidl, out pdwAttrib); desktopFolder.BindToObject(pidl, IntPtr.Zero, ref IID_IShellFolder, ref someFolder); //Get the file//s IExtractImage someFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, shortFileName, out cParsed, out filePidl, out pdwAttrib); object iunk = null; IntPtr[] pidl_array = new IntPtr[] { filePidl }; someFolder.GetUIObjectOf(IntPtr.Zero, 1, pidl_array, ref IID_IExtractImage, IntPtr.Zero, out iunk); extract = (IExtractImage)iunk; //Set the size SIZE size; size.cx = DesiredSize.Width; size.cy = DesiredSize.Height; StringBuilder location = new StringBuilder(260, 260); int priority = 0; int requestedColourDepth = 32; // The IEIFLAG_ORIGSIZE flag tells it to use the original aspect // ratio for the image size. The IEIFLAG_QUALITY flag tells the // interface we want the image to be the best possible quality. EIEIFLAG flags = EIEIFLAG.IEIFLAG_ORIGSIZE | EIEIFLAG.IEIFLAG_QUALITY; // EIEIFLAG.IEIFLAG_ASPECT;// EIEIFLAG.IEIFLAG_SCREEN; //EIEIFLAG flags = EIEIFLAG.IEIFLAG_ORIGSIZE | EIEIFLAG.IEIFLAG_QUALITY; int uFlags = (int)flags; IntPtr bmp; //Interop will throw an exception if one of these calls fail. try { extract.GetLocation(location, location.Capacity, ref priority, ref size, requestedColourDepth, ref uFlags); extract.Extract(out bmp); if (!bmp.Equals(IntPtr.Zero)) { return(Image.FromHbitmap(bmp)); } else { return(null); } } catch { return(null); } finally { //Free the pidls. The Runtime Callable Wrappers should automatically release the COM objects Marshal.FreeCoTaskMem(pidl); Marshal.FreeCoTaskMem(filePidl); } }
private bool GetThumbnailHelper(string file, IntPtr pidl, IShellFolder item) { IntPtr bitmapPointer = IntPtr.Zero; IExtractImage extractImage = null; try { string pidlPath = this.PathFromPidl(pidl); if (Path.GetFileName(pidlPath).ToUpper().Equals(Path.GetFileName(file).ToUpper())) { IUnknown iunk = null; int prgf = 0; Guid iidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1"); item.GetUIObjectOf(IntPtr.Zero, 1, ref pidl, ref iidExtractImage, ref prgf, ref iunk); extractImage = (IExtractImage)iunk; if (extractImage != null) { Console.WriteLine("Got an IExtractImage object - " + file); SIZE sz = new SIZE(); sz.HorizontalSize = this.DesiredSize.Width; sz.VerticalSize = this.DesiredSize.Height; StringBuilder location = new StringBuilder(260, 260); int priority = 0; int requestedColourDepth = 32; EIEIFLAG flags = EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_SCREEN; int nameFlags = (int)flags; extractImage.GetLocation(location, location.Capacity, ref priority, ref sz, requestedColourDepth, ref nameFlags); extractImage.Extract(ref bitmapPointer); if (bitmapPointer != IntPtr.Zero) { this.thumbNail = Bitmap.FromHbitmap(bitmapPointer); } Marshal.ReleaseComObject(extractImage); extractImage = null; } return(true); } else { return(false); } } catch (Exception ex) { if (bitmapPointer != IntPtr.Zero) { UnmanagedMethods.DeleteObject(bitmapPointer); } if (extractImage != null) { Marshal.ReleaseComObject(extractImage); } throw ex; } }
public Bitmap LoadThumbnailFromExtractImage(string filename, Size requestedSize) { if (!System.IO.Directory.Exists(filename) && !System.IO.File.Exists(filename)) { return(null); } Bitmap bm1 = null; IntPtr hbitmap = IntPtr.Zero; SIZE sz = new SIZE(requestedSize.Width, requestedSize.Height); IShellFolder desktop = null; IntPtr pidlParent = IntPtr.Zero; IShellFolder shellFolder = null; IUnknown iunk = null; IExtractImage extractImage = null; try { Debug.Write("Trying IExtractImage\r\n"); Shell32.SHGetDesktopFolder(ref desktop); int cParsed = 0; int pdwAttrib = 0; string filePath = filename; if (System.IO.File.Exists(filename)) { filePath = System.IO.Path.GetDirectoryName(filename); } desktop.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filePath, out cParsed, out pidlParent, out pdwAttrib); Guid uuidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046"); desktop.BindToObject(pidlParent, IntPtr.Zero, ref uuidShellFolder, out shellFolder); Guid uuidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1"); iunk = GetUIObject(shellFolder, filename, uuidExtractImage); if (iunk == null) { Debug.Write(" IExtractImage not supported\r\n"); return(null); } extractImage = (IExtractImage)iunk; StringBuilder location = new StringBuilder(260, 260); int priority = 0; EIEIFLAG flags = EIEIFLAG.IEIFLAG_ORIGSIZE; int uFlags = (int)flags; extractImage.GetLocation(location, location.Capacity, ref priority, ref sz, 32, ref uFlags); extractImage.Extract(out hbitmap); bm1 = GetBitmapFromHbitmap(hbitmap); } catch (Exception e) { Debug.Write(" Exception extracting image from IExtractImage: " + e.Message + "\r\n"); } finally { if (extractImage != null) { Marshal.ReleaseComObject(extractImage); extractImage = null; } if (iunk != null) { Marshal.ReleaseComObject(iunk); iunk = null; } if (shellFolder != null) { Marshal.ReleaseComObject(shellFolder); shellFolder = null; } if (pidlParent != IntPtr.Zero) { Allocator.Free(pidlParent); pidlParent = IntPtr.Zero; } if (desktop != null) { Marshal.ReleaseComObject(desktop); desktop = null; } if (_allocator != null) { Marshal.ReleaseComObject(_allocator); _allocator = null; } } return(bm1); }
private bool getThumbNail(string file, IntPtr pidl, IShellFolder item) { IntPtr hBmp = IntPtr.Zero; IExtractImage extractImage = null; try { var pidlPath = pathFromPidl(pidl); if (!Path.GetFileName(pidlPath).ToUpper().Equals(Path.GetFileName(file).ToUpper())) { return(false); } IUnknown iunk = null; var prgf = 0; var iidExtractImage = new Guid("BB2E617C-0920-11d1-9A0B-00C04FC2D6C1"); item.GetUIObjectOf(IntPtr.Zero, 1, ref pidl, ref iidExtractImage, ref prgf, ref iunk); extractImage = (IExtractImage)iunk; if (extractImage == null) { return(true); } var sz = new Size { cx = DesiredSize.Width, cy = DesiredSize.Height }; var location = new StringBuilder(260, 260); var priority = 0; const int requestedColourDepth = 32; const EIEIFLAG flags = EIEIFLAG.IeiflagScreen | EIEIFLAG.IeiflagAsync | EIEIFLAG.IeiflagQuality | EIEIFLAG.IeiflagCache; var uFlags = (int)flags; try { extractImage.GetLocation( location, location.Capacity, ref priority, ref sz, requestedColourDepth, ref uFlags); } catch { } extractImage.Extract(ref hBmp); if (hBmp != IntPtr.Zero) { ThumbNail = Image.FromHbitmap(hBmp); } Marshal.ReleaseComObject(extractImage); extractImage = null; return(true); } finally { if (hBmp != IntPtr.Zero) { UnmanagedMethods.DeleteObject(hBmp); } if (extractImage != null) { Marshal.ReleaseComObject(extractImage); } } }
//========================================================== //renvoie la miniature (ou à défaut l'icône) du fichier szFileName //========================================================= //IN szFileName : nom du fichier dont on veut la miniature //IN dwCX : largeur de la miniature //IN dwCY : hauteur de la miniature //IN allowIcon: some videos don't have a true thumbnail. // if true, it will return the icon of the default program used to open this kind of file // (the icon of VLC for example) //renvoie une instance d'image VB IPictureDisp //========================================================== private Bitmap ExtractImage(string szFileName, int dwCX, int dwCY, bool allowIcon = true) { int priority = 0; int requestedColourDepth; int flags; SIZE sz; IntPtr pidl = IntPtr.Zero; IShellFolder isf = null; IExtractImage ie = null; IExtractIcon ii = null; StringBuilder szPath; int pindex = 0; IntPtr pIconLarge = IntPtr.Zero; IntPtr pIconSmall = IntPtr.Zero; SHFILEINFO shgfi = new SHFILEINFO(); Bitmap ret = null; requestedColourDepth = 32; flags = (int)(EIEIFLAG.IEIFLAG_ASPECT | EIEIFLAG.IEIFLAG_OFFLINE | EIEIFLAG.IEIFLAG_SCREEN); //on récupère le nom de fichier sous forme ITEMLIST (pidl) et le dossier parent sous forme IShellFolder isf = GetShellFolder(szFileName, ref pidl); // Try to get the thumbnail ie = getThumbnail(isf, pidl); // If not possible if (ie == null) { // The icon ii = getIcon(isf, pidl); // If possible if (!(ii == null)) { // Extract the icon szPath = new StringBuilder(260); ii.GetIconLocation(0, szPath, 260, ref pindex, ref flags); ii.Extract(szPath.ToString(), pindex, ref pIconLarge, ref pIconSmall, dwCX + 65536 * dwCX); if (pIconLarge == IntPtr.Zero) { SHGetFileInfo(szFileName, 0, ref shgfi, Marshal.SizeOf(shgfi), SHGFIFLAGS.SHGFI_LARGEICON | SHGFIFLAGS.SHGFI_ICON | SHGFIFLAGS.SHGFI_OVERLAYINDEX); ret = Icon.FromHandle(shgfi.hIcon).ToBitmap(); } else { ret = Icon.FromHandle(pIconLarge).ToBitmap(); } } else { SHGetFileInfo(szFileName, 0, ref shgfi, Marshal.SizeOf(shgfi), SHGFIFLAGS.SHGFI_LARGEICON | SHGFIFLAGS.SHGFI_ICON | SHGFIFLAGS.SHGFI_OVERLAYINDEX); ret = Icon.FromHandle(shgfi.hIcon).ToBitmap(); } } else // If possible // Extract the thumbnail with the wanted dimensions { sz.cx = dwCX; sz.cy = dwCY; szPath = new StringBuilder(260); EIEIFLAG f = (EIEIFLAG)flags; int r = ie.GetLocation(szPath, szPath.Capacity, out priority, ref sz, requestedColourDepth, ref f); try { pIconLarge = ie.Extract(); } catch { } // If not possible if (pIconLarge == IntPtr.Zero) { // Retry to get the icon ii = getIcon(isf, pidl); // If possible if (!(ii == null)) { szPath = new StringBuilder(260); ii.GetIconLocation(0, szPath, 260, ref pindex, ref flags); ii.Extract(szPath.ToString(), pindex, ref pIconLarge, ref pIconSmall, dwCX + 65536 * dwCX); if (pIconLarge == IntPtr.Zero) { SHGetFileInfo(szFileName, 0, ref shgfi, Marshal.SizeOf(shgfi), SHGFIFLAGS.SHGFI_LARGEICON | SHGFIFLAGS.SHGFI_ICON); ret = Icon.FromHandle(shgfi.hIcon).ToBitmap(); } else { if (!allowIcon) { return(null); } ret = Icon.FromHandle(pIconLarge).ToBitmap(); } } else { SHGetFileInfo(szFileName, 0, ref shgfi, Marshal.SizeOf(shgfi), SHGFIFLAGS.SHGFI_LARGEICON | SHGFIFLAGS.SHGFI_ICON); ret = Icon.FromHandle(shgfi.hIcon).ToBitmap(); } } else { ret = (Bitmap)Bitmap.FromHbitmap(pIconLarge); } } ILFree(pidl); if (ret == null) { ret.MakeTransparent(); } if (pIconLarge != IntPtr.Zero) { DeleteObject(pIconLarge); } if (pIconSmall != IntPtr.Zero) { DeleteObject(pIconSmall); } if (isf != null) { Marshal.ReleaseComObject(isf); } if (ie != null) { Marshal.ReleaseComObject(ie); } if (ii != null) { Marshal.ReleaseComObject(ii); } return(ret); }