Skip to content

Commit

Permalink
Fixed image and video preview crash (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
shibayan authored Aug 27, 2020
1 parent a17213c commit 54d9fd1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 19 deletions.
34 changes: 27 additions & 7 deletions WinQuickLook/Handlers/ImageQuickLookHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
}
33 changes: 24 additions & 9 deletions WinQuickLook/Handlers/VideoQuickLookHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,41 @@ 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();

videoViewer.BeginInit();
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<string> _supportFormats = new[]
Expand Down
2 changes: 1 addition & 1 deletion WinQuickLook/QuickLookWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<Window.Resources>
<ResourceDictionary Source="Styles.xaml" />
Expand Down
4 changes: 2 additions & 2 deletions WinQuickLook/WinQuickLook.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@

<ItemGroup>
<PackageReference Include="AvalonEdit" Version="6.0.1" />
<PackageReference Include="Microsoft.AppCenter.Analytics" Version="3.4.0" />
<PackageReference Include="Microsoft.AppCenter.Crashes" Version="3.4.0" />
<PackageReference Include="Microsoft.AppCenter.Analytics" Version="3.4.1" />
<PackageReference Include="Microsoft.AppCenter.Crashes" Version="3.4.1" />
<PackageReference Include="Microsoft.Windows.SDK.Contracts" Version="10.0.18362.2005" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="PdfiumViewer" Version="2.13.0">
Expand Down

0 comments on commit 54d9fd1

Please sign in to comment.