From ab6e3a02ea1cd48155fc6bff452fbb3865c1e128 Mon Sep 17 00:00:00 2001 From: pizzaboxer Date: Sat, 21 Sep 2024 20:28:57 +0100 Subject: [PATCH] Handle exception in Icon to ImageSource conversion --- Bloxstrap/Extensions/IconEx.cs | 21 +++++++++++++++++++-- Bloxstrap/Resources/Strings.Designer.cs | 11 +++++++++++ Bloxstrap/Resources/Strings.resx | 5 +++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/Bloxstrap/Extensions/IconEx.cs b/Bloxstrap/Extensions/IconEx.cs index 2cd48af7..516899dc 100644 --- a/Bloxstrap/Extensions/IconEx.cs +++ b/Bloxstrap/Extensions/IconEx.cs @@ -8,11 +8,28 @@ public static class IconEx { public static Icon GetSized(this Icon icon, int width, int height) => new(icon, new Size(width, height)); - public static ImageSource GetImageSource(this Icon icon) + public static ImageSource GetImageSource(this Icon icon, bool handleException = true) { using MemoryStream stream = new(); icon.Save(stream); - return BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); + + if (handleException) + { + try + { + return BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); + } + catch (Exception ex) + { + App.Logger.WriteException("IconEx::GetImageSource", ex); + Frontend.ShowMessageBox(String.Format(Strings.Dialog_IconLoadFailed, ex.Message)); + return BootstrapperIcon.IconBloxstrap.GetIcon().GetImageSource(false); + } + } + else + { + return BitmapFrame.Create(stream, BitmapCreateOptions.None, BitmapCacheOption.OnLoad); + } } } } diff --git a/Bloxstrap/Resources/Strings.Designer.cs b/Bloxstrap/Resources/Strings.Designer.cs index 40c1f30f..56f7a4c4 100644 --- a/Bloxstrap/Resources/Strings.Designer.cs +++ b/Bloxstrap/Resources/Strings.Designer.cs @@ -976,6 +976,17 @@ public static string Dialog_Exception_Title { } } + /// + /// Looks up a localized string similar to The chosen bootstrapper icon could not be loaded. + /// + ///{0}. + /// + public static string Dialog_IconLoadFailed { + get { + return ResourceManager.GetString("Dialog.IconLoadFailed", resourceCulture); + } + } + /// /// Looks up a localized string similar to Choose preferred language. /// diff --git a/Bloxstrap/Resources/Strings.resx b/Bloxstrap/Resources/Strings.resx index 51165198..99adfd73 100644 --- a/Bloxstrap/Resources/Strings.resx +++ b/Bloxstrap/Resources/Strings.resx @@ -1198,4 +1198,9 @@ Please manually delete Bloxstrap.exe from the install location or try restarting Failed to save {0}: {1} + + The chosen bootstrapper icon could not be loaded. + +{0} + \ No newline at end of file