bloxstrap/Bloxstrap/Extensions/IconEx.cs
pvvv ddbcda4f0b
Slight fix for IconEx.cs
double checks the stream position is reset before reading
2025-04-11 13:27:58 -05:00

38 lines
1.2 KiB
C#

using System.Drawing;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows.Media;
namespace Bloxstrap.Extensions
{
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, bool handleException = true)
{
using MemoryStream stream = new();
icon.Save(stream);
stream.Seek(0, SeekOrigin.Begin);
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);
}
}
}
}