From 54d9fd16c9b615869907601aaf3cc94488032f10 Mon Sep 17 00:00:00 2001 From: Tatsuro Shibamura Date: Thu, 27 Aug 2020 23:04:38 +0900 Subject: [PATCH] Fixed image and video preview crash (#47) --- .../Handlers/ImageQuickLookHandler.cs | 34 +++++++++++++++---- .../Handlers/VideoQuickLookHandler.cs | 33 +++++++++++++----- WinQuickLook/QuickLookWindow.xaml | 2 +- WinQuickLook/WinQuickLook.csproj | 4 +-- 4 files changed, 54 insertions(+), 19 deletions(-) diff --git a/WinQuickLook/Handlers/ImageQuickLookHandler.cs b/WinQuickLook/Handlers/ImageQuickLookHandler.cs index ebc7de7..4866c58 100644 --- a/WinQuickLook/Handlers/ImageQuickLookHandler.cs +++ b/WinQuickLook/Handlers/ImageQuickLookHandler.cs @@ -75,21 +75,41 @@ private static (BitmapSource, Size) GetImage(string fileName) private static (Size, Size) GetScaledImageSize(string fileName, int maxSize) { - using var tag = TagLib.File.Create(fileName); + if (!TryGetImageSize(fileName, out var originalSize)) + { + using var stream = File.OpenRead(fileName); - var width = tag.Properties.PhotoWidth; - var height = tag.Properties.PhotoHeight; + var decoder = BitmapDecoder.Create(stream, BitmapCreateOptions.DelayCreation, BitmapCacheOption.None); - var originalSize = new Size(width, height); + originalSize = new Size(decoder.Frames[0].PixelWidth, decoder.Frames[0].PixelHeight); + } - if (width > maxSize || height > maxSize) + if (originalSize.Width > maxSize || originalSize.Height > maxSize) { - var scaleFactor = (double)maxSize / Math.Max(width, height); + var scaleFactor = maxSize / Math.Max(originalSize.Width, originalSize.Height); - return (new Size(width * scaleFactor, height * scaleFactor), originalSize); + return (new Size(originalSize.Width * scaleFactor, originalSize.Height * scaleFactor), originalSize); } return (originalSize, originalSize); } + + private static bool TryGetImageSize(string fileName, out Size size) + { + try + { + using var tag = TagLib.File.Create(fileName); + + size = new Size(tag.Properties.PhotoWidth, tag.Properties.PhotoHeight); + + return true; + } + catch + { + size = default; + + return false; + } + } } } diff --git a/WinQuickLook/Handlers/VideoQuickLookHandler.cs b/WinQuickLook/Handlers/VideoQuickLookHandler.cs index 4a01b7b..b05cbbc 100644 --- a/WinQuickLook/Handlers/VideoQuickLookHandler.cs +++ b/WinQuickLook/Handlers/VideoQuickLookHandler.cs @@ -20,13 +20,10 @@ public bool CanOpen(string fileName) public async Task<(FrameworkElement, Size, string)> GetViewerAsync(string fileName) { - using var file = TagLib.File.Create(fileName); - - var requestSize = new Size + if (!TryGetVideoSize(fileName, out var requestSize)) { - Width = file.Properties.VideoWidth, - Height = file.Properties.VideoHeight - }; + requestSize = new Size(); + } var videoViewer = new VideoFileViewer(); @@ -34,12 +31,30 @@ public bool CanOpen(string fileName) videoViewer.Source = new Uri(fileName, UriKind.Absolute); videoViewer.EndInit(); - return (videoViewer, requestSize, FormatMetadata(file, fileName)); + return (videoViewer, requestSize, FormatMetadata(requestSize, fileName)); } - private static string FormatMetadata(TagLib.File file, string fileName) + private static string FormatMetadata(Size size, string fileName) { - return $"{file.Properties.VideoWidth}x{file.Properties.VideoHeight} - {WinExplorerHelper.GetFileSize(fileName)}"; + return $"{size.Width}x{size.Height} - {WinExplorerHelper.GetFileSize(fileName)}"; + } + + private static bool TryGetVideoSize(string fileName, out Size size) + { + try + { + using var tag = TagLib.File.Create(fileName); + + size = new Size(tag.Properties.VideoWidth, tag.Properties.VideoHeight); + + return true; + } + catch + { + size = default; + + return false; + } } private static readonly IList _supportFormats = new[] diff --git a/WinQuickLook/QuickLookWindow.xaml b/WinQuickLook/QuickLookWindow.xaml index 052423b..a172f43 100644 --- a/WinQuickLook/QuickLookWindow.xaml +++ b/WinQuickLook/QuickLookWindow.xaml @@ -5,7 +5,7 @@ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" x:Name="window" d:DesignWidth="600" d:DesignHeight="400" MinWidth="400" MinHeight="400" Background="Transparent" WindowStyle="None" - ShowActivated="False" ShowInTaskbar="False" SnapsToDevicePixels="True" UseLayoutRounding="True" FocusVisualStyle="{x:Null}" + ShowActivated="False" ShowInTaskbar="False" SnapsToDevicePixels="True" UseLayoutRounding="True" FocusVisualStyle="{x:Null}" WindowState="Normal" Unloaded="Window_Unloaded" SizeChanged="Window_SizeChanged"> diff --git a/WinQuickLook/WinQuickLook.csproj b/WinQuickLook/WinQuickLook.csproj index 64788fe..82f51c9 100644 --- a/WinQuickLook/WinQuickLook.csproj +++ b/WinQuickLook/WinQuickLook.csproj @@ -19,8 +19,8 @@ - - + +