//http://msdn.microsoft.com/en-us/library/bb761848(VS.85).aspx

        public static bool GetIExtractImage(
            IShellFolder2 parent,
            IntPtr filePidl,
            out IntPtr iExtractImagePtr,
            out IExtractImage iExtractImage)
        {
            if (parent.GetUIObjectOf(
                    IntPtr.Zero,
                    1,
                    new IntPtr[] { filePidl },
                    ref ShellAPI.IID_IExtractImage,
                    IntPtr.Zero,
                    out iExtractImagePtr) == ShellAPI.S_OK)
            {
                iExtractImage =
                    (IExtractImage)Marshal.GetTypedObjectForIUnknown(
                        iExtractImagePtr, typeof(IExtractImage));

                return(true);
            }
            else
            {
                iExtractImagePtr = IntPtr.Zero;
                iExtractImage    = null;

                return(false);
            }
        }
        private static bool isVistaUp = Environment.OSVersion.Version.Major >= 6; //5 = XP, 6 = Vista

        //http://msdn.microsoft.com/en-us/library/bb761848(VS.85).aspx

        public static bool GetIExtractImage(
            IShellFolder2 parent,
            IntPtr filePidl,
            out IntPtr iExtractImagePtr,
            out IExtractImage iExtractImage)
        {
            if (!isVistaUp)
            {
                throw new NotSupportedException("Require Vista or above.");
            }

            if (parent.GetUIObjectOf(
                    IntPtr.Zero,
                    1,
                    new IntPtr[] { filePidl },
                    ref ShellAPI.IID_IExtractImage,
                    IntPtr.Zero,
                    out iExtractImagePtr) == ShellAPI.S_OK)
            {
                iExtractImage =
                    (IExtractImage)Marshal.GetTypedObjectForIUnknown(
                        iExtractImagePtr, typeof(IExtractImage));

                return(true);
            }
            else
            {
                iExtractImagePtr = IntPtr.Zero;
                iExtractImage    = null;

                return(false);
            }
        }
        //http://msdn.microsoft.com/en-us/library/bb761848(VS.85).aspx
        public static bool GetIExtractImage(
           IShellFolder2 parent,
            IntPtr filePidl,
           out IntPtr iExtractImagePtr,
           out IExtractImage iExtractImage)
        {
            if (parent.GetUIObjectOf(
                        IntPtr.Zero,
                        1,
                        new IntPtr[] { filePidl },
                        ref ShellAPI.IID_IExtractImage,
                        IntPtr.Zero,
                        out iExtractImagePtr) == ShellAPI.S_OK)
            {
                iExtractImage =
                    (IExtractImage)Marshal.GetTypedObjectForIUnknown(
                        iExtractImagePtr, typeof(IExtractImage));

                return true;
            }
            else
            {
                iExtractImagePtr = IntPtr.Zero;
                iExtractImage = null;

                return false;
            }
        }
        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;
            }
        }
Beispiel #5
0
        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;
            }
        }
        public static Bitmap ExtractImage(FileInfoEx entry, Size size, bool quality)
        {
            try
            {
                IntPtr        iExtractImagePtr = IntPtr.Zero;
                IExtractImage iExtractImage    = null;


                if (GetIExtractImage(entry, out iExtractImagePtr, out iExtractImage))
                {
                    try
                    {
                        ShellDll.ShellAPI.SIZE prgSize = new ShellAPI.SIZE()
                        {
                            cx = size.Width, cy = size.Height
                        };
                        IExtractImageFlags flags = IExtractImageFlags.Cache | IExtractImageFlags.Aspect;
                        if (quality)
                        {
                            flags |= IExtractImageFlags.Quality;
                        }
                        StringBuilder location = new StringBuilder(260, 260);

                        try
                        {
                            int pdwPriority = 1;
                            iExtractImage.GetLocation(location,
                                                      location.Capacity, ref pdwPriority, ref prgSize, 32, ref flags);
                        }
                        catch (COMException) { }

                        IntPtr ptrBitmapImage = IntPtr.Zero;
                        iExtractImage.Extract(out ptrBitmapImage);
                        if (ptrBitmapImage != IntPtr.Zero)
                        {
                            return(Bitmap.FromHbitmap(ptrBitmapImage));
                        }
                    }
                    catch (Exception) { }
                }
                finally
                {
                    if (iExtractImage != null)
                    {
                        Marshal.ReleaseComObject(iExtractImage);
                    }

                    if (iExtractImagePtr != IntPtr.Zero)
                    {
                        Marshal.Release(iExtractImagePtr);
                    }
                }

                return(null);
            }
 public static bool GetIExtractImage(
     FileInfoEx file,
     out IntPtr iExtractImagePtr,
     out IExtractImage iExtractImage)
 {
     using (ShellFolder2 sf2 = file.Parent.ShellFolder)
     {
         PIDL filePIDL = file.PIDLRel;
         try
         {
             return(GetIExtractImage(sf2, filePIDL.Ptr, out iExtractImagePtr, out iExtractImage));
         }
         finally { filePIDL.Free(); }
     }
 }
Beispiel #8
0
        private void GenerateIExtractImage(string path)
        {
            try {
                // we get the desktop shell then the PIDL for the file's folder.
                // Once we have the PIDL then get a reference to the folder (BindToObject) then we can get the PIDL for the file.
                //Now get the IExtractImage interface (GETUIObjectOf) from which we can cast to the IExtractImage object
                SHGetDesktopFolder(out _desktopFolder);

                int pdwAttributes = 0; // not required
                int pchEaten      = 0; // not required
                _hResult = _desktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, System.IO.Path.GetDirectoryName(path), out pchEaten, out _folderPidl, out pdwAttributes);
                if (_hResult != S_OK)
                {
                    Marshal.ThrowExceptionForHR(_hResult);
                }

                _hResult = _desktopFolder.BindToObject(_folderPidl, IntPtr.Zero, ref _iidShellFolder, ref _folder);
                if (_hResult != S_OK)
                {
                    Marshal.ThrowExceptionForHR(_hResult);
                }

                _hResult = _folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, System.IO.Path.GetFileName(path), out pchEaten, out _filePidl, out pdwAttributes);
                if (_hResult != S_OK)
                {
                    Marshal.ThrowExceptionForHR(_hResult);
                }

                IUnknown unk = null;
                _hResult = _folder.GetUIObjectOf(IntPtr.Zero, 1, ref _filePidl, ref _iidExtractImage, out _reserved, ref unk);
                if (_hResult != S_OK)
                {
                    Marshal.ThrowExceptionForHR(_hResult);
                }

                // Now cast the unknown as the extractImage object
                _extractImage = (IExtractImage)unk;
            } catch (FileNotFoundException) {
            } catch (COMException) {
            } catch (Exception) {
            }
        }
 private void GenerateIExtractImage()
 {
     try
     {
         ShellMgr.SHGetDesktopFolder(out this._desktopFolder);
         int num  = 0;
         int num2 = 0;
         this.hResult = this._desktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, System.IO.Path.GetDirectoryName(this._path), out num2, out this._folderPidl, out num);
         if (this.hResult != 0)
         {
             Marshal.ThrowExceptionForHR(this.hResult);
         }
         this.hResult = this._desktopFolder.BindToObject(this._folderPidl, IntPtr.Zero, ref this.iidShellFolder, ref this._folder);
         if (this.hResult != 0)
         {
             Marshal.ThrowExceptionForHR(this.hResult);
         }
         this.hResult = this._folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, System.IO.Path.GetFileName(this._path), out num2, out this._filePidl, out num);
         if (this.hResult != 0)
         {
             Marshal.ThrowExceptionForHR(this.hResult);
         }
         IUnknown unknown = null;
         this.hResult = this._folder.GetUIObjectOf(IntPtr.Zero, 1, ref this._filePidl, ref this.iidExtractImage, out this._reserved, ref unknown);
         if (this.hResult != 0)
         {
             Marshal.ThrowExceptionForHR(this.hResult);
         }
         this._extractImage = (IExtractImage)unknown;
     }
     catch (FileNotFoundException)
     {
     }
     catch (COMException)
     {
     }
     catch (Exception)
     {
     }
 }
        public static Bitmap ExtractImage(FileInfoEx entry, Size size, bool quality)
        {
            IntPtr        iExtractImagePtr = IntPtr.Zero;
            IExtractImage iExtractImage    = null;


            if (GetIExtractImage(entry, out iExtractImagePtr, out iExtractImage))
            {
                try
                {
                    ShellDll.ShellAPI.SIZE prgSize = new ShellAPI.SIZE()
                    {
                        cx = size.Width, cy = size.Height
                    };
                    IExtractImageFlags flags = IExtractImageFlags.Async | IExtractImageFlags.Aspect;
                    if (quality)
                    {
                        flags |= IExtractImageFlags.Quality;
                    }
                    StringBuilder location = new StringBuilder(260, 260);

                    try
                    {
                        iExtractImage.GetLocation(location,
                                                  location.Capacity, 1, prgSize, 32, flags);
                    }
                    catch (COMException) { }

                    IntPtr ptrBitmapImage = IntPtr.Zero;
                    iExtractImage.Extract(out ptrBitmapImage);
                    if (ptrBitmapImage != IntPtr.Zero)
                    {
                        return(Bitmap.FromHbitmap(ptrBitmapImage));
                    }
                }
                catch (Exception) { }
            }
        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;
            }
        }
Beispiel #12
0
 public static bool GetIExtractImage(
    FileInfoEx file,
    out IntPtr iExtractImagePtr,
    out IExtractImage iExtractImage)
 {
     using (ShellFolder2 sf2 = file.Parent.ShellFolder)
     {
         PIDL filePIDL = file.PIDLRel;
         try
         {
             return GetIExtractImage(sf2, filePIDL.Ptr, out iExtractImagePtr, out iExtractImage);
         }
         finally { filePIDL.Free(); }
     }
 }
        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);
            }
        }
Beispiel #14
0
        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;
            }
        }
Beispiel #15
0
        private static ImageData LoadThumbnail2(string path, DateTime dtLastWriteTime, out Size sizeRaw, out Size sizeActual, out string toolTipText, out bool fCached)
        {
            sizeRaw     = sizeActual = Size.Empty;
            toolTipText = null;
            fCached     = false;
            IntPtr       zero = IntPtr.Zero;
            IShellFolder ppv  = null;
            object       obj2 = null;

            try {
                IntPtr ptr3;
                zero = PInvoke.ILCreateFromPath(path);
                if ((zero != IntPtr.Zero) && (PInvoke.SHBindToParent(zero, ExplorerGUIDs.IID_IShellFolder, out ppv, out ptr3) == 0))
                {
                    uint     rgfReserved = 0;
                    Guid     riid        = ExplorerGUIDs.IID_IExtractImage;
                    IntPtr[] apidl       = new IntPtr[] { ptr3 };
                    if (ppv.GetUIObjectOf(IntPtr.Zero, 1, apidl, ref riid, ref rgfReserved, out obj2) == 0)
                    {
                        IntPtr        ptr2;
                        IExtractImage image         = (IExtractImage)obj2;
                        StringBuilder pszPathBuffer = new StringBuilder(260);
                        int           pdwPriority   = 0;
                        Size          prgSize       = new Size(QTUtility.PreviewMaxWidth, QTUtility.PreviewMaxHeight);
                        int           pdwFlags      = 0x60;
                        if (((image.GetLocation(pszPathBuffer, pszPathBuffer.Capacity, ref pdwPriority, ref prgSize, 0x18, ref pdwFlags) == 0) && (image.Extract(out ptr2) == 0)) && (ptr2 != IntPtr.Zero))
                        {
                            Bitmap bmp  = Image.FromHbitmap(ptr2);
                            Size   size = bmp.Size;
                            sizeRaw = sizeActual = size;
                            ImageData data = new ImageData(bmp, null, path, dtLastWriteTime, size, size);
                            data.Thumbnail = true;
                            try {
                                toolTipText = data.TooltipText = ShellMethods.GetShellInfoTipText(zero, false);
                            }
                            catch {
                            }
                            return(data);
                        }
                    }
                }
            }
            catch (Exception exception) {
                QTUtility2.MakeErrorLog(exception, null);
            }
            finally {
                if (zero != IntPtr.Zero)
                {
                    PInvoke.CoTaskMemFree(zero);
                }
                if (ppv != null)
                {
                    Marshal.ReleaseComObject(ppv);
                }
                if (obj2 != null)
                {
                    Marshal.ReleaseComObject(obj2);
                }
            }
            return(null);
        }
Beispiel #16
0
        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 void GenerateIExtractImage(string path)
        {
            try {
            // we get the desktop shell then the PIDL for the file's folder.
            // Once we have the PIDL then get a reference to the folder (BindToObject) then we can get the PIDL for the file.
            //Now get the IExtractImage interface (GETUIObjectOf) from which we can cast to the IExtractImage object
            SHGetDesktopFolder(out _desktopFolder);

            int pdwAttributes = 0; // not required
            int pchEaten = 0; // not required
            _hResult = _desktopFolder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, System.IO.Path.GetDirectoryName(path), out pchEaten, out _folderPidl, out pdwAttributes);
            if (_hResult != S_OK) Marshal.ThrowExceptionForHR(_hResult);

            _hResult = _desktopFolder.BindToObject(_folderPidl, IntPtr.Zero, ref _iidShellFolder, ref _folder);
            if (_hResult != S_OK) Marshal.ThrowExceptionForHR(_hResult);

            _hResult = _folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, System.IO.Path.GetFileName(path), out pchEaten, out _filePidl, out pdwAttributes);
            if (_hResult != S_OK) Marshal.ThrowExceptionForHR(_hResult);

            IUnknown unk = null;
            _hResult = _folder.GetUIObjectOf(IntPtr.Zero, 1, ref _filePidl, ref _iidExtractImage, out _reserved, ref unk);
            if (_hResult != S_OK) Marshal.ThrowExceptionForHR(_hResult);

            // Now cast the unknown as the extractImage object
            _extractImage = (IExtractImage) unk;
              } catch (FileNotFoundException) {
              } catch (COMException) {
              } catch (Exception) {
              }
        }
Beispiel #18
0
        //http://msdn.microsoft.com/en-us/library/bb761848(VS.85).aspx
        public static bool GetIExtractImage(
           IShellFolder2 parent,
            IntPtr filePidl,
           out IntPtr iExtractImagePtr,
           out IExtractImage iExtractImage)
        {
            if (!isVistaUp)
                throw new NotSupportedException("Require Vista or above.");

            if (parent.GetUIObjectOf(
                        IntPtr.Zero,
                        1,
                        new IntPtr[] { filePidl },
                        ref ShellAPI.IID_IExtractImage,
                        IntPtr.Zero,
                        out iExtractImagePtr) == ShellAPI.S_OK)
            {
                iExtractImage =
                    (IExtractImage)Marshal.GetTypedObjectForIUnknown(
                        iExtractImagePtr, typeof(IExtractImage));

                return true;
            }
            else
            {
                iExtractImagePtr = IntPtr.Zero;
                iExtractImage = null;

                return false;
            }
        }
Beispiel #19
0
    //==========================================================
    //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);
    }
Beispiel #20
0
        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);
                }
            }
        }