Skip to content

Commit

Permalink
Fix for images with bad dpi
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonyCorbett committed Oct 19, 2019
1 parent 768c053 commit 4b534b0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
41 changes: 40 additions & 1 deletion OnlyM.CoreSys/GraphicsUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
}
}
}
17 changes: 2 additions & 15 deletions OnlyM/Services/ImageDisplayManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion SolutionInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

[assembly: AssemblyVersion("1.5.0.18")]
[assembly: AssemblyVersion("1.5.0.19")]

0 comments on commit 4b534b0

Please sign in to comment.