From 4b534b040ee83d956f0fe6f82847c092ade57e46 Mon Sep 17 00:00:00 2001 From: AntonyCorbett Date: Sat, 19 Oct 2019 15:39:49 +0100 Subject: [PATCH] Fix for images with bad dpi --- OnlyM.CoreSys/GraphicsUtils.cs | 41 ++++++++++++++++++++++++++- OnlyM/Services/ImageDisplayManager.cs | 17 ++--------- SolutionInfo.cs | 2 +- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/OnlyM.CoreSys/GraphicsUtils.cs b/OnlyM.CoreSys/GraphicsUtils.cs index e07719924..930f3c133 100644 --- a/OnlyM.CoreSys/GraphicsUtils.cs +++ b/OnlyM.CoreSys/GraphicsUtils.cs @@ -16,6 +16,7 @@ public static class GraphicsUtils { + private const int MaxDpi = 1200; private static readonly object _tagLibLocker = new object(); public static bool AutoRotateIfRequired(string itemFilePath) @@ -101,7 +102,10 @@ public static BitmapSource Downsize(string imageFilePath, int maxImageWidth, int return Downsize(image, maxImageWidth, maxImageHeight); } - public static BitmapSource Downsize(BitmapSource image, int maxImageWidth, int maxImageHeight) + public static BitmapSource Downsize( + BitmapSource image, + int maxImageWidth, + int maxImageHeight) { var factorWidth = (double)maxImageWidth / image.PixelWidth; var factorHeight = (double)maxImageHeight / image.PixelHeight; @@ -245,6 +249,13 @@ public static BitmapImage GetBitmapImageWithCacheOnLoad(string imageFile) bmp.CacheOption = BitmapCacheOption.OnLoad; bmp.EndInit(); + if (IsBadDpi(bmp)) + { + // NB - if the DpiX and DpiY metadata is bad then the bitmap can't be displayed + // correctly, so fix it here... + return FixBadDpi(imageFile); + } + return bmp; } @@ -450,5 +461,33 @@ private static bool ImageRequiresRotation(string imageFilePath) return false; } + + private static bool IsBadDpi(BitmapImage bmp) + { + return bmp.DpiX > MaxDpi || bmp.DpiY > MaxDpi; + } + + private static BitmapImage FixBadDpi(string imageFile) + { + using (var imageFactory = new ImageFactory()) + { + using (MemoryStream outStream = new MemoryStream()) + { + imageFactory + .Load(imageFile) + .Resolution(96, 96) + .Save(outStream); + + var bitmapImage = new BitmapImage(); + + bitmapImage.BeginInit(); + bitmapImage.CacheOption = BitmapCacheOption.OnLoad; + bitmapImage.StreamSource = outStream; + bitmapImage.EndInit(); + + return bitmapImage; + } + } + } } } diff --git a/OnlyM/Services/ImageDisplayManager.cs b/OnlyM/Services/ImageDisplayManager.cs index 1698ad2ac..03d16d333 100644 --- a/OnlyM/Services/ImageDisplayManager.cs +++ b/OnlyM/Services/ImageDisplayManager.cs @@ -10,13 +10,13 @@ using System.Windows.Controls; using System.Windows.Media; using System.Windows.Media.Animation; - using System.Windows.Media.Imaging; using System.Windows.Threading; using GalaSoft.MvvmLight.Threading; using OnlyM.Core.Extensions; using OnlyM.Core.Models; using OnlyM.Core.Services.Options; using OnlyM.Core.Utils; + using OnlyM.CoreSys; using OnlyM.Models; using OnlyM.Services.ImagesCache; using OnlyM.Slides; @@ -373,7 +373,7 @@ private void ShowImageInControl(string imageFile, Image imageCtrl, ImageFadeType var imageSrc = _optionsService.CacheImages ? ImageCache.GetImage(imageFile) - : GetBitmapImageWithCacheOnLoad(imageFile); + : GraphicsUtils.GetBitmapImageWithCacheOnLoad(imageFile); if (!shouldFadeIn) { @@ -414,19 +414,6 @@ private void ShowImageInControl(string imageFile, Image imageCtrl, ImageFadeType } } - private BitmapImage GetBitmapImageWithCacheOnLoad(string imageFile) - { - var bmp = new BitmapImage(); - - bmp.BeginInit(); - bmp.CreateOptions = BitmapCreateOptions.IgnoreColorProfile; - bmp.UriSource = new Uri(imageFile); - bmp.CacheOption = BitmapCacheOption.OnLoad; - bmp.EndInit(); - - return bmp; - } - private void InitFromSlideshowFile(string mediaItemFilePath) { var sf = new SlideFile(mediaItemFilePath); diff --git a/SolutionInfo.cs b/SolutionInfo.cs index cb8024840..11ae3b258 100644 --- a/SolutionInfo.cs +++ b/SolutionInfo.cs @@ -6,4 +6,4 @@ [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] -[assembly: AssemblyVersion("1.5.0.18")] \ No newline at end of file +[assembly: AssemblyVersion("1.5.0.19")] \ No newline at end of file