Repository: microsoft/PowerToys
File: src/modules/peek/Peek.FilePreviewer/Previewers/ImagePreviewer/ImagePreviewer.cs (C#)

Code before:
            return await WICHelper.GetImageSize(File.Path);
        }

        public Task LoadPreviewAsync()
        {
            var lowQualityThumbnailTask = LoadLowQualityThumbnailAsync();
            var highQualityThumbnailTask = LoadHighQualityThumbnailAsync();
            var fullImageTask = LoadFullQualityImageAsync();

            return Task.WhenAll(lowQualityThumbnailTask, highQualityThumbnailTask, fullImageTask);
        }

        private Task LoadLowQualityThumbnailAsync()
        {
            var thumbnailTCS = new TaskCompletionSource();
            Dispatcher.TryEnqueue(async () =>
            {
                if (CancellationToken.IsCancellationRequested)
                {
                    _cancellationTokenSource = new CancellationTokenSource();
                    return;
                }

                if (!IsFullImageLoaded && !IsHighQualityThumbnailLoaded)
                {
                    if (File.Extension == ".png")
                    {
                        // TODO: special condition for PNG for faster rendering using WinRT APIs
                        Preview = await ThumbnailHelper.GetThumbnailAsync(File.Path, 256);
                    }
                    else
                    {
                        // TODO: Handle thumbnail errors
                        ThumbnailHelper.GetThumbnail(Path.GetFullPath(File.Path), out IntPtr hbitmap, ThumbnailHelper.LowQualityThumbnailSize);
                        var thumbnailBitmap = await GetBitmapFromHBitmapAsync(hbitmap);
                        Preview = thumbnailBitmap;
                    }
                }

                thumbnailTCS.SetResult();
            });

            return thumbnailTCS.Task;
        }

        private Task LoadHighQualityThumbnailAsync()
        {
            var thumbnailTCS = new TaskCompletionSource();
            Dispatcher.TryEnqueue(async () =>
            {
                if (CancellationToken.IsCancellationRequested)

Review comment (suggestion):
I think we might want to introduce a `PngPreviewer` in the future instead of having conditional logic in here! Could be a separate pr.

Code after:
        public async Task<Size> GetPreviewSizeAsync()
        {
            var propertyImageSize = await PropertyHelper.GetImageSize(File.Path);
            if (propertyImageSize != Size.Empty)
            {
                return propertyImageSize;
            }

            return await WICHelper.GetImageSize(File.Path);
        }

        public async Task LoadPreviewAsync()
        {
            State = PreviewState.Loading;

            LowQualityThumbnailTask = LoadLowQualityThumbnailAsync();
            HighQualityThumbnailTask = LoadHighQualityThumbnailAsync();
            FullQualityImageTask = LoadFullQualityImageAsync();

            await Task.WhenAll(LowQualityThumbnailTask, HighQualityThumbnailTask, FullQualityImageTask);

            if (Preview == null && HasFailedLoadingPreview())
            {
                State = PreviewState.Error;
            }
        }

        private void OnPropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (e.PropertyName == nameof(Preview))
            {
                if (Preview != null)
                {
                    State = PreviewState.Loaded;
                }
            }
        }

        private Task<bool> LoadLowQualityThumbnailAsync()
        {
            return TaskExtension.RunSafe(async () =>
            {
                if (CancellationToken.IsCancellationRequested)
                {
                    _cancellationTokenSource = new CancellationTokenSource();
                    return;
                }

                if (!IsFullImageLoaded && !IsHighQualityThumbnailLoaded)
                {
                    var hr = ThumbnailHelper.GetThumbnail(Path.GetFullPath(File.Path), out IntPtr hbitmap, ThumbnailHelper.LowQualityThumbnailSize);