예제 #1
0
        /// <summary>
        /// Handles the DoWork event of the queued background worker.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ImageGlass.ImageListView.QueuedWorkerDoWorkEventArgs"/> instance
        /// containing the event data.</param>
        void bw_DoWork(object sender, QueuedWorkerDoWorkEventArgs e)
        {
            string extension = e.Argument as string;

            // Should we continue processing this item?
            // The callback checks if the item is already cached.
            if (!OnCanContinueProcessing(extension))
            {
                e.Cancel = true;
                return;
            }

            // Read shell info
            ShellInfoExtractor info = ShellInfoExtractor.FromFile(extension);

            // Return the info
            CacheItem result = null;

            if ((info.SmallIcon == null || info.LargeIcon == null) && !RetryOnError)
            {
                result = new CacheItem(extension, info.SmallIcon, info.LargeIcon, info.FileType, CacheState.Error);
            }
            else
            {
                result = new CacheItem(extension, info.SmallIcon, info.LargeIcon, info.FileType, CacheState.Cached);
            }

            e.Result = result;
        }
        /// <summary>
        /// Creates an instance of the ShellInfoExtractor class.
        /// </summary>
        /// <param name="path">Filepath of image</param>
        public static ShellInfoExtractor FromFile(string path)
        {
            ShellInfoExtractor info = new ShellInfoExtractor();

            try
            {
                SHFILEINFO shinfo     = new SHFILEINFO();
                uint       structSize = (uint)Marshal.SizeOf(shinfo);
                SHGFI      flags      = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.TypeName | SHGFI.UseFileAttributes;

                // Get the small icon and shell file type
                IntPtr hImg = SHGetFileInfo(path, FileAttributes.Normal, out shinfo,
                                            structSize, flags);

                // Get mime type
                info.FileType = shinfo.szTypeName;

                // Get small icon
                if (hImg != IntPtr.Zero && shinfo.hIcon != IntPtr.Zero)
                {
                    using (Icon newIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon))
                    {
                        info.SmallIcon = newIcon.ToBitmap();
                    }
                    DestroyIcon(shinfo.hIcon);
                }
                else
                {
                    info.Error = new Exception("Error reading shell icon");
                }

                // Get large icon
                hImg = SHGetFileInfo(path, FileAttributes.Normal, out shinfo,
                                     structSize, SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes);

                if (hImg != IntPtr.Zero && shinfo.hIcon != IntPtr.Zero)
                {
                    using (Icon newIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon))
                    {
                        info.LargeIcon = newIcon.ToBitmap();
                    }
                    DestroyIcon(shinfo.hIcon);
                }
                else
                {
                    info.Error = new Exception("Error reading shell icon");
                }
            }
            catch (Exception e)
            {
                info.Error = e;
            }

            return(info);
        }
예제 #3
0
        /// <summary>
        /// Creates an instance of the ShellInfoExtractor class.
        /// </summary>
        /// <param name="path">Filepath of image</param>
        public static ShellInfoExtractor FromFile(string path)
        {
            ShellInfoExtractor info = new ShellInfoExtractor();

            try
            {
                SHFILEINFO shinfo = new SHFILEINFO();
                uint structSize = (uint)Marshal.SizeOf(shinfo);
                SHGFI flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.TypeName | SHGFI.UseFileAttributes;

                // Get the small icon and shell file type
                IntPtr hImg = SHGetFileInfo(path, FileAttributes.Normal, out shinfo,
                    structSize, flags);

                // Get mime type
                info.FileType = shinfo.szTypeName;

                // Get small icon
                if (hImg != IntPtr.Zero && shinfo.hIcon != IntPtr.Zero)
                {
                    using (Icon newIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon))
                    {
                        info.SmallIcon = newIcon.ToBitmap();
                    }
                    DestroyIcon(shinfo.hIcon);
                }
                else
                    info.Error = new Exception("Error reading shell icon");

                // Get large icon
                hImg = SHGetFileInfo(path, FileAttributes.Normal, out shinfo,
                    structSize, SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes);

                if (hImg != IntPtr.Zero && shinfo.hIcon != IntPtr.Zero)
                {
                    using (Icon newIcon = System.Drawing.Icon.FromHandle(shinfo.hIcon))
                    {
                        info.LargeIcon = newIcon.ToBitmap();
                    }
                    DestroyIcon(shinfo.hIcon);
                }
                else
                    info.Error = new Exception("Error reading shell icon");
            }
            catch (Exception e)
            {
                info.Error = e;
            }

            return info;
        }