diff --git a/src/Converters/UriToBitmapConverter.cs b/src/Converters/UriToBitmapConverter.cs index d01f74f..6b77dec 100644 --- a/src/Converters/UriToBitmapConverter.cs +++ b/src/Converters/UriToBitmapConverter.cs @@ -22,9 +22,13 @@ class UriToBitmapConverter : IValueConverter { bi.UriSource = new Uri(path); bi.EndInit(); + if (bi.CanFreeze) + { + bi.Freeze(); + } return bi; } - catch + catch { return null; } diff --git a/src/Services/CoverService.cs b/src/Services/CoverService.cs index f692184..24ae789 100644 --- a/src/Services/CoverService.cs +++ b/src/Services/CoverService.cs @@ -110,7 +110,7 @@ public static async Task GetCover(List codices) } } - public static void SaveCover(IMagickImage image, Codex destCodex) + public static void SaveCover(Codex destCodex, IMagickImage image) { if (String.IsNullOrEmpty(destCodex.CoverArt)) { @@ -121,8 +121,7 @@ public static void SaveCover(IMagickImage image, Codex destCodex) if (image.Width > 850) image.Resize(850, 0); image.Write(destCodex.CoverArt); - image.Dispose(); - CreateThumbnail(destCodex); + CreateThumbnail(destCodex, image); } public static async Task SaveCover(string imgURL, Codex destCodex) @@ -136,6 +135,7 @@ public static async Task SaveCover(string imgURL, Codex destCodex) var imgBytes = await IOService.DownloadFileAsync(imgURL); await File.WriteAllBytesAsync(destCodex.CoverArt, imgBytes); CreateThumbnail(destCodex); + destCodex.RefreshThumbnail(); } public static bool GetCoverFromImage(string imagePath, Codex destCodex) @@ -156,10 +156,13 @@ public static bool GetCoverFromImage(string imagePath, Codex destCodex) try { - using MagickImage image = new(imagePath); - if (image.Width > 1000) image.Resize(1000, 0); - image.Write(destCodex.CoverArt); - CreateThumbnail(destCodex); + using (MagickImage image = new(imagePath)) + { + if (image.Width > 1000) image.Resize(1000, 0); + image.Write(destCodex.CoverArt); + CreateThumbnail(destCodex, image); + } + destCodex.RefreshThumbnail(); return true; } catch (Exception ex) @@ -170,25 +173,41 @@ public static bool GetCoverFromImage(string imagePath, Codex destCodex) } } - public static void CreateThumbnail(Codex c) + public static void CreateThumbnail(Codex c, IMagickImage? image = null) { - if (String.IsNullOrEmpty(c.CoverArt)) + if (String.IsNullOrEmpty(c.Thumbnail)) { Logger.Error("Trying to write thumbnail to empty path", new InvalidOperationException()); return; } uint newWidth = 200; //sets resolution of thumbnail in pixels - if (!Path.Exists(c.CoverArt)) return; - using MagickImage image = new(c.CoverArt); - //preserve aspect ratio - uint width = image.Width; - uint height = image.Height; - uint newHeight = newWidth / width * height; - //create thumbnail - image.Thumbnail(newWidth, newHeight); - image.Write(c.Thumbnail); - c.RefreshThumbnail(); + bool ownsImage = false; + + if (image is null) + { + if (!Path.Exists(c.CoverArt)) return; + image = new MagickImage(c.CoverArt); + ownsImage = true; + } + + try + { + //preserve aspect ratio + uint width = image.Width; + uint height = image.Height; + uint newHeight = newWidth / width * height; + //create thumbnail + image.Thumbnail(newWidth, newHeight); + image.Write(c.Thumbnail); + } + finally + { + if (ownsImage) + { + image?.Dispose(); + } + } } //Take screenshot of specific html element diff --git a/src/ViewModels/SettingsViewModel.cs b/src/ViewModels/SettingsViewModel.cs index 58a0bb8..36000f8 100644 --- a/src/ViewModels/SettingsViewModel.cs +++ b/src/ViewModels/SettingsViewModel.cs @@ -687,6 +687,7 @@ public void RegenAllThumbnails() { //codex.Thumbnail = codex.CoverArt.Replace("CoverArt", "Thumbnails"); CoverService.CreateThumbnail(codex); + codex.RefreshThumbnail(); } } diff --git a/src/ViewModels/Sources/GmBinderSourceViewModel.cs b/src/ViewModels/Sources/GmBinderSourceViewModel.cs index 4dcf86c..9b4679f 100644 --- a/src/ViewModels/Sources/GmBinderSourceViewModel.cs +++ b/src/ViewModels/Sources/GmBinderSourceViewModel.cs @@ -58,8 +58,11 @@ public override async Task FetchCover(Codex codex) await Task.Run(() => driver.Navigate().GoToUrl(codex.Sources.SourceURL)); var coverPage = driver.FindElement(OpenQA.Selenium.By.Id("p1")); //screenshot and download the image - IMagickImage image = CoverService.GetCroppedScreenShot(driver, coverPage); - CoverService.SaveCover(image, codex); + using (IMagickImage image = CoverService.GetCroppedScreenShot(driver, coverPage)) + { + CoverService.SaveCover(codex, image); + } + codex.RefreshThumbnail(); return true; } catch (Exception ex) diff --git a/src/ViewModels/Sources/HomebrewerySourceViewModel.cs b/src/ViewModels/Sources/HomebrewerySourceViewModel.cs index 8830c3d..2c77e14 100644 --- a/src/ViewModels/Sources/HomebrewerySourceViewModel.cs +++ b/src/ViewModels/Sources/HomebrewerySourceViewModel.cs @@ -105,8 +105,11 @@ await Task.Run(() => location.Y += coverPage.Location.Y; //screenshot and download the image - IMagickImage image = CoverService.GetCroppedScreenShot(driver, location, coverPage.Size); - CoverService.SaveCover(image, codex); + using (IMagickImage image = CoverService.GetCroppedScreenShot(driver, location, coverPage.Size)) + { + CoverService.SaveCover(codex, image); + } + codex.RefreshThumbnail(); return true; } catch (Exception ex) diff --git a/src/ViewModels/Sources/PdfSourceViewModel.cs b/src/ViewModels/Sources/PdfSourceViewModel.cs index 1fe91e4..1ff6c9c 100644 --- a/src/ViewModels/Sources/PdfSourceViewModel.cs +++ b/src/ViewModels/Sources/PdfSourceViewModel.cs @@ -89,14 +89,17 @@ public override async Task FetchCover(Codex codex) try //image.Read can throw exception if file can not be opened/read { - using MagickImage image = new(); - await image.ReadAsync(codex.Sources.Path, ReadSettings); - image.Format = MagickFormat.Png; - image.BackgroundColor = new MagickColor("#000000"); //set background color as transparent - image.Trim(); //cut off all transparency + using (MagickImage image = new()) + { + await image.ReadAsync(codex.Sources.Path, ReadSettings); + image.Format = MagickFormat.Png; + image.BackgroundColor = new MagickColor("#000000"); //set background color as transparent + image.Trim(); //cut off all transparency - await image.WriteAsync(codex.CoverArt); - CoverService.CreateThumbnail(codex); + await image.WriteAsync(codex.CoverArt); + CoverService.CreateThumbnail(codex, image); + } + codex.RefreshThumbnail(); return true; } catch (Exception ex)