Fix bug where book with corrupt image cannot be queued.

This commit is contained in:
MBucari 2023-03-11 20:40:02 -07:00
parent 245e55782e
commit 5ec01913d5
11 changed files with 58 additions and 67 deletions

View file

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using DataLayer;
@ -42,7 +41,7 @@ namespace LibationWinForms.Dialogs
this.Text = Book.Title;
(_, var picture) = PictureStorage.GetPicture(new PictureDefinition(Book.PictureId, PictureSize._80x80));
this.coverPb.Image = Dinah.Core.WindowsDesktop.Drawing.ImageReader.ToImage(picture);
this.coverPb.Image = WinFormsUtil.TryLoadImageOrDefault(picture, PictureSize._80x80);
var t = @$"
Title: {Book.Title}

View file

@ -19,15 +19,7 @@ namespace LibationWinForms.GridView
public void SetCoverArt(byte[] cover)
{
try
{
pictureBox1.Image = Dinah.Core.WindowsDesktop.Drawing.ImageReader.ToImage(cover);
}
catch (Exception ex)
{
Serilog.Log.Logger.Error(ex, "Error loading cover art for {file}", PictureFileName);
pictureBox1.Image = Properties.Resources.default_cover_500x500;
}
pictureBox1.Image = WinFormsUtil.TryLoadImageOrDefault(cover);
}
#region Make the form's aspect ratio always match the picture's aspect ratio.

View file

@ -1,7 +1,5 @@
using DataLayer;
using Dinah.Core.WindowsDesktop.Drawing;
using LibationUiBase.GridView;
using System;
using System.Drawing;
namespace LibationWinForms.GridView
@ -14,23 +12,12 @@ namespace LibationWinForms.GridView
private WinFormsEntryStatus(LibraryBook libraryBook) : base(libraryBook) { }
public static EntryStatus Create(LibraryBook libraryBook) => new WinFormsEntryStatus(libraryBook);
protected override object LoadImage(byte[] picture)
{
try
{
return ImageReader.ToImage(picture);
}
catch (Exception ex)
{
Serilog.Log.Logger.Error(ex, "Error loading cover art for {Book}", Book);
return Properties.Resources.default_cover_80x80;
}
}
protected override Image LoadImage(byte[] picture)
=> WinFormsUtil.TryLoadImageOrDefault(picture, LibationFileManager.PictureSize._80x80);
protected override Image GetResourceImage(string rescName)
{
var image = Properties.Resources.ResourceManager.GetObject(rescName);
return image as Bitmap;
}
}

View file

@ -12,7 +12,6 @@ using AudibleApi;
using DataLayer;
using Dinah.Core;
using Dinah.Core.ErrorHandling;
using Dinah.Core.WindowsDesktop.Drawing;
using FileLiberator;
using LibationFileManager;
using LibationUiBase;
@ -87,7 +86,7 @@ namespace LibationWinForms.ProcessQueue
if (isDefault)
PictureStorage.PictureCached += PictureStorage_PictureCached;
_cover = ImageReader.ToImage(picture);
_cover = WinFormsUtil.TryLoadImageOrDefault(picture, PictureSize._80x80); ;
}
@ -95,7 +94,7 @@ namespace LibationWinForms.ProcessQueue
{
if (e.Definition.PictureId == LibraryBook.Book.PictureId)
{
Cover = ImageReader.ToImage(e.Picture);
Cover = WinFormsUtil.TryLoadImageOrDefault(e.Picture, PictureSize._80x80);
PictureStorage.PictureCached -= PictureStorage_PictureCached;
}
}
@ -260,7 +259,7 @@ namespace LibationWinForms.ProcessQueue
private void AudioDecodable_CoverImageDiscovered(object sender, byte[] coverArt)
{
Cover = ImageReader.ToImage(coverArt);
Cover = WinFormsUtil.TryLoadImageOrDefault(coverArt, PictureSize._80x80);
}
#endregion

View file

@ -0,0 +1,23 @@
using Dinah.Core.WindowsDesktop.Drawing;
using LibationFileManager;
using System.Drawing;
namespace LibationWinForms
{
internal static class WinFormsUtil
{
private static Bitmap defaultImage;
public static Image TryLoadImageOrDefault(byte[] picture, PictureSize defaultSize = PictureSize.Native)
{
try
{
return ImageReader.ToImage(picture);
}
catch
{
using var ms = new System.IO.MemoryStream(PictureStorage.GetDefaultImage(defaultSize));
return defaultImage ??= new Bitmap(ms);
}
}
}
}