diff --git a/TwilightBoxart.UX/App.config b/TwilightBoxart.UX/App.config index d5b3839..e1f388e 100644 --- a/TwilightBoxart.UX/App.config +++ b/TwilightBoxart.UX/App.config @@ -1,33 +1,33 @@ - + - + - - + + - - + + - - + + - - + + - - + + - + diff --git a/TwilightBoxart.UX/TwilightBoxart.UX.csproj b/TwilightBoxart.UX/TwilightBoxart.UX.csproj index f9a88d9..dd9247a 100644 --- a/TwilightBoxart.UX/TwilightBoxart.UX.csproj +++ b/TwilightBoxart.UX/TwilightBoxart.UX.csproj @@ -1,6 +1,6 @@  - + @@ -87,8 +87,8 @@ ..\packages\System.Numerics.Vectors.4.5.0\lib\net46\System.Numerics.Vectors.dll - - ..\packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll + + ..\packages\System.Runtime.CompilerServices.Unsafe.4.6.0\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll @@ -178,14 +178,13 @@ - This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - + + goto :$(ConfigurationName) @@ -201,4 +200,5 @@ goto :exit :Debug :exit + \ No newline at end of file diff --git a/TwilightBoxart.UX/packages.config b/TwilightBoxart.UX/packages.config index 654edcb..091eef6 100644 --- a/TwilightBoxart.UX/packages.config +++ b/TwilightBoxart.UX/packages.config @@ -1,16 +1,16 @@  - - + + - - - + + + diff --git a/TwilightBoxart/Helpers/ImgDownloader.cs b/TwilightBoxart/Helpers/ImgDownloader.cs index e5fc2b0..0ad3895 100644 --- a/TwilightBoxart/Helpers/ImgDownloader.cs +++ b/TwilightBoxart/Helpers/ImgDownloader.cs @@ -1,5 +1,11 @@ -using System.Net; +using System.IO; +using System.Net; using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Advanced; +using SixLabors.ImageSharp.Formats; +using SixLabors.ImageSharp.Formats.Gif; +using SixLabors.ImageSharp.Formats.Jpeg; +using SixLabors.ImageSharp.Formats.Png; using SixLabors.ImageSharp.Processing; using SixLabors.Primitives; @@ -14,21 +20,69 @@ public ImgDownloader(int width, int height) { _width = width; _height = height; - } + } public void DownloadAndResize(string url, string targetFile) { using (var webClient = new WebClient()) { var data = webClient.DownloadData(url); - using (var image = Image.Load(data)) + var decoder = GetDecoder(url); + + using (var image = decoder == null ? Image.Load(data) : Image.Load(data, decoder)) { + image.Metadata.ExifProfile = null; + image.Mutate(x => x.Resize(_width, _height)); - image.Save(targetFile); + + var encoder = GetEncoder(image, targetFile); + image.Save(targetFile, encoder); } } } + private IImageDecoder GetDecoder(string sourceFile) + { + var ext = Path.GetExtension(sourceFile)?.ToLower(); + + switch (ext) + { + case ".png": + return new PngDecoder { IgnoreMetadata = true }; + case ".jpg": + case ".jpeg": + return new JpegDecoder { IgnoreMetadata = true }; + case ".gif": + return new GifDecoder { IgnoreMetadata = true }; + default: + return null; + } + } + + private IImageEncoder GetEncoder(Image image, string targetFile) + { + var ext = Path.GetExtension(targetFile); + var manager = image.GetConfiguration().ImageFormatsManager; + var format = manager.FindFormatByFileExtension(ext); + var encoder = manager.FindEncoder(format); + + if (encoder is PngEncoder pngEncoder) + { + SetPngSettings(pngEncoder); + } + + return encoder; + } + + private void SetPngSettings(PngEncoder encoder) + { + encoder.CompressionLevel = 9; + encoder.InterlaceMethod = PngInterlaceMode.None; + encoder.BitDepth = PngBitDepth.Bit8; + encoder.ColorType = PngColorType.Rgb; + encoder.FilterMethod = PngFilterMethod.Adaptive; + } + public void SetSizeAdjustedToAspectRatio(Size aspectRatio) { if (_width == aspectRatio.Width || _height == aspectRatio.Height) diff --git a/TwilightBoxart/Models/Base/LibRetroRom.cs b/TwilightBoxart/Models/Base/LibRetroRom.cs index 6bded1e..b66a1d1 100644 --- a/TwilightBoxart/Models/Base/LibRetroRom.cs +++ b/TwilightBoxart/Models/Base/LibRetroRom.cs @@ -1,4 +1,5 @@ -using KirovAir.Core.Extensions; +using System; +using KirovAir.Core.Extensions; using KirovAir.Core.Utilities; namespace TwilightBoxart.Models.Base @@ -56,7 +57,7 @@ private void DownloadWithRetry(string name, string targetFile) { Download(ConsoleType, name, targetFile); } - catch + catch (Exception e) { if (NoIntroConsoleType == ConsoleType.Unknown || ConsoleType == NoIntroConsoleType) throw;