Esempio n. 1
0
        public void Dispose()
        {
            if (_disposed)
            {
                return;
            }

            if (_alloc != null)
            {
                Marshal.ReleaseComObject(_alloc);
            }
            _alloc = null;
            if (ThumbNail != null)
            {
                ThumbNail.Dispose();
            }
            _disposed = true;
        }
Esempio n. 2
0
        public Bitmap GetThumbnail(string fileName)
        {
            if (!File.Exists(fileName) && !Directory.Exists(fileName))
            {
                throw new FileNotFoundException(string.Format("The file '{0}' does not exist", fileName), fileName);
            }

            if (ThumbNail != null)
            {
                ThumbNail.Dispose();
                ThumbNail = null;
            }

            var folder = getDesktopFolder;

            if (folder == null)
            {
                return(ThumbNail);
            }

            var pidlMain = IntPtr.Zero;

            try
            {
                var cParsed   = 0;
                var pdwAttrib = 0;
                var filePath  = Path.GetDirectoryName(fileName);
                folder.ParseDisplayName(IntPtr.Zero, IntPtr.Zero, filePath, ref cParsed, ref pidlMain, ref pdwAttrib);
            }
            catch (Exception)
            {
                freeResources(pidlMain, folder, null, null);
                throw;
            }

            if (pidlMain == IntPtr.Zero)
            {
                freeResources(pidlMain, folder, null, null);
                return(null);
            }


            var          iidShellFolder = new Guid("000214E6-0000-0000-C000-000000000046");
            IShellFolder item           = null;

            try
            {
                folder.BindToObject(pidlMain, IntPtr.Zero, ref iidShellFolder, ref item);
            }
            catch (Exception)
            {
                freeResources(pidlMain, folder, null, item);
                throw;
            }

            if (item == null)
            {
                freeResources(pidlMain, folder, null, null);
                return(null);
            }

            IEnumIDList idEnum = null;

            try
            {
                item.EnumObjects(IntPtr.Zero, (Eshcontf.ShcontfFolders | Eshcontf.ShcontfNonfolders), ref idEnum);
            }
            catch (Exception)
            {
                freeResources(pidlMain, folder, idEnum, item);
                throw;
            }

            if (idEnum == null)
            {
                freeResources(pidlMain, folder, null, item);
                return(null);
            }

            var pidl     = IntPtr.Zero;
            var fetched  = 0;
            var complete = false;

            while (!complete)
            {
                var hRes = idEnum.Next(1, ref pidl, ref fetched);
                if (hRes != 0)
                {
                    pidl     = IntPtr.Zero;
                    complete = true;
                }
                else
                {
                    if (getThumbNail(fileName, pidl, item))
                    {
                        complete = true;
                    }
                }

                if (pidl != IntPtr.Zero)
                {
                    allocator.Free(pidl);
                }
            }

            freeResources(pidlMain, folder, idEnum, item);
            return(ThumbNail);
        }