using System; using System.Drawing; using System.Globalization; using System.Net; using System.Windows; using System.Windows.Data; using System.Windows.Media; using System.Windows.Media.Imaging; using Brush = System.Windows.Media.Brush; using Brushes = System.Windows.Media.Brushes; using FontFamily = System.Windows.Media.FontFamily; using Point = System.Windows.Point; namespace ModernWpfPlayground.PropertyPresenter2 { /// /// Makes an Bitmap from every Imageformat. /// public sealed class ObjectImageConverter : IValueConverter { object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture) { const string dynResPrefix = "dynRes:"; if (value is Bitmap bitmap) { return System.Windows.Interop.Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromWidthAndHeight(16, 16)); } if (!(value is string strValue)) return Binding.DoNothing; if (strValue.StartsWith(dynResPrefix, StringComparison.Ordinal)) { var resource = Application.Current.TryFindResource(strValue.Replace(dynResPrefix, string.Empty)); return resource is ImageSource source ? source : Binding.DoNothing; } if (strValue.StartsWith("text:", StringComparison.Ordinal)) { var parts = strValue.Split(':'); return parts.Length == 3 ? DrawText(WebUtility.HtmlDecode(parts[2]), parts[1], Brushes.Black) : Binding.DoNothing; } return GetIcon(Geometry.Parse(strValue), null); } object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing; /// /// Get icon a ImageSource from geometry. /// /// geometry /// color of the icon /// public static ImageSource GetIcon(Geometry geometry, Brush brush) { if (brush == null) brush = Brushes.Black; var drawing = new GeometryDrawing(brush, null, geometry); return new DrawingImage(drawing); } /// /// Draw text as ImageSource from string. /// /// the text /// font of the string /// color of the string /// public static ImageSource DrawText(string text, string strFontFamily, Brush brush) { var fontFamily = new FontFamily(strFontFamily); var formattedText = new FormattedText(text, CultureInfo.GetCultureInfo("en-us"), FlowDirection.LeftToRight, new Typeface( fontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal), 64, brush); var geometry = formattedText.BuildGeometry(new Point(0, 0)); return GetIcon(geometry, null); } } /// /// Invert boolean converter /// [ValueConversion(typeof(bool), typeof(bool))] public class InverseBooleanConverter : IValueConverter { /// public object Convert(object value, Type targetType, object parameter, CultureInfo culture) => value is bool b ? !b : throw new InvalidOperationException("The target must be a boolean"); /// public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) => Binding.DoNothing; } }