Handle exception in Icon to ImageSource conversion

This commit is contained in:
pizzaboxer 2024-09-21 20:28:57 +01:00
parent f844a654f6
commit ab6e3a02ea
No known key found for this signature in database
GPG Key ID: 59D4A1DBAD0F2BA8
3 changed files with 35 additions and 2 deletions

View File

@ -8,11 +8,28 @@ namespace Bloxstrap.Extensions
{
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);
}
}
}
}

View File

@ -976,6 +976,17 @@ namespace Bloxstrap.Resources {
}
}
/// <summary>
/// Looks up a localized string similar to The chosen bootstrapper icon could not be loaded.
///
///{0}.
/// </summary>
public static string Dialog_IconLoadFailed {
get {
return ResourceManager.GetString("Dialog.IconLoadFailed", resourceCulture);
}
}
/// <summary>
/// Looks up a localized string similar to Choose preferred language.
/// </summary>

View File

@ -1198,4 +1198,9 @@ Please manually delete Bloxstrap.exe from the install location or try restarting
<data name="Bootstrapper.JsonManagerSaveFailed" xml:space="preserve">
<value>Failed to save {0}: {1}</value>
</data>
<data name="Dialog.IconLoadFailed" xml:space="preserve">
<value>The chosen bootstrapper icon could not be loaded.
{0}</value>
</data>
</root>