Utopia-Canteen-System/Converters/StringToImageSourceConverte...

36 lines
1009 B
C#

using System;
using System.Globalization;
using System.Windows.Data;
using System.Windows.Media.Imaging;
namespace UtopiaCanteenSystem.Converters
{
/// <summary>
/// Converts a string URI (e.g. pack:// or file path) to an ImageSource for Image controls.
/// </summary>
public class StringToImageSourceConverter : IValueConverter
{
public object? Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var path = value?.ToString();
if (string.IsNullOrWhiteSpace(path))
return null;
try
{
var uri = new Uri(path, UriKind.RelativeOrAbsolute);
return new BitmapImage(uri);
}
catch
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}