2022-10-23 11:02:31 +13:00
|
|
|
|
// <copyright file="IconToImageSourceConverter.cs" company="PlaceholderCompany">
|
|
|
|
|
// Copyright (c) PlaceholderCompany. All rights reserved.
|
|
|
|
|
// </copyright>
|
|
|
|
|
//
|
2023-04-30 10:06:00 +12:00
|
|
|
|
// Copyright (c) 2022-2023 Peter Kirmeier
|
2022-10-23 11:02:31 +13:00
|
|
|
|
|
|
|
|
|
namespace SystemTrayMenu.Utilities
|
|
|
|
|
{
|
|
|
|
|
using System;
|
|
|
|
|
using System.Drawing;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Windows;
|
|
|
|
|
using System.Windows.Data;
|
|
|
|
|
using System.Windows.Interop;
|
|
|
|
|
using System.Windows.Markup;
|
|
|
|
|
using System.Windows.Media;
|
|
|
|
|
using System.Windows.Media.Imaging;
|
|
|
|
|
|
|
|
|
|
[ValueConversion(typeof(Icon), typeof(ImageSource))]
|
2023-05-20 06:43:19 +12:00
|
|
|
|
[ValueConversion(typeof(Icon), typeof(BitmapSource))]
|
2022-10-23 11:02:31 +13:00
|
|
|
|
public class IconToImageSourceConverter : MarkupExtension, IValueConverter
|
|
|
|
|
{
|
|
|
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
if (value != null)
|
|
|
|
|
{
|
|
|
|
|
// Invalid Icon happens usually only during design time with dummy default data
|
2023-04-30 10:06:00 +12:00
|
|
|
|
Icon icon = value is Icon validIcon ? validIcon : SystemIcons.Error;
|
2022-10-23 11:02:31 +13:00
|
|
|
|
return Imaging.CreateBitmapSourceFromHIcon(
|
|
|
|
|
icon.Handle,
|
|
|
|
|
Int32Rect.Empty,
|
|
|
|
|
BitmapSizeOptions.FromEmptyOptions());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return null!;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override object ProvideValue(IServiceProvider serviceProvider)
|
|
|
|
|
{
|
|
|
|
|
return this;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|