Пример #1
0
        /// <summary>
        /// Loads an associated icon from a file located in the specified filePath
        /// </summary>
        /// <param name="filePath">The path to the file from which to load the associated icon</param>
        /// <param name="iconHash">The output icon hash string</param>
        /// <returns></returns>
        private static BitmapSource LoadIcon(string filePath, out string iconHash)
        {
            Icon icon = null;

            // The LoadIcon method can be called from not the main thread but GetFileIcon have to be called only
            // from the main thread so we need to always use our Dispatcher object to invoke GetFileIcon method
            Dispatcher.Invoke(() => icon = FileToIconConverter.GetFileIcon(filePath));
            BitmapSource image = FileToIconConverter.LoadBitmap(icon);

            if (image.CanFreeze)
            {
                image.Freeze();
            }

            var tempBmp   = icon.ToBitmap();
            var thumbnail = GetThumbnail(tempBmp);

            iconHash = GetImageHash(thumbnail);

            thumbnail?.Dispose();
            tempBmp?.Dispose();
            icon?.Dispose();

            return(image);
        }
Пример #2
0
        /// <summary>
        /// Loads an icon from an image file located in the specified filePath
        /// </summary>
        /// <param name="filePath">The path to the image file from which to load the icon</param>
        /// <param name="iconHash">The output icon hash string</param>
        /// <returns></returns>
        private static ImageSource LoadIconFromImageFile(string filePath, out string iconHash)
        {
            Bitmap originalImage = null;
            Bitmap resizedImage  = null;
            Bitmap thumbnail     = null;

            try
            {
                originalImage = new Bitmap(filePath);
                float aspectRatio = originalImage.Width / (float)originalImage.Height;
                int   newWidth    = DefaultIconSize;
                int   newHeight   = (int)(DefaultIconSize / aspectRatio);
                resizedImage = new Bitmap(originalImage, newWidth, newHeight);

                ImageSource resultImage = FileToIconConverter.LoadBitmap(resizedImage);

                if (resultImage.CanFreeze)
                {
                    resultImage.Freeze();
                }

                thumbnail = GetThumbnail(resizedImage);
                iconHash  = GetImageHash(thumbnail);

                return(resultImage);
            }
            catch
            {
                iconHash = string.Empty;
                return(null);
            }
            finally
            {
                originalImage?.Dispose();
                resizedImage?.Dispose();
                thumbnail?.Dispose();
            }
        }