JD2022-TU1/main/tools/framework/JD.Timeline/Dance/PictogramImageConverter.cs

57 lines
1.8 KiB
C#

using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using JD.Timeline.Model.Clips;
using JD.Core.Drawing;
using JD.Serialization;
namespace JD.Timeline.View.Converters
{
public class PictogramImageConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (targetType != typeof(ImageSource))
throw new InvalidOperationException("The target must be a ImageSource");
var model = (PictogramModel)value;
try
{
Bitmap bitmap = model.BitmapHandler().BitmapThumbnail;
if (bitmap == null)
return null;
lock (bitmap)
{
var resizedBitmap = ImageUtility.ResizeImage(bitmap, 42);
using (var memory = new MemoryStream())
{
resizedBitmap.Save(memory, ImageFormat.Png);
memory.Position = 0;
var bitmapImage = new BitmapImage();
bitmapImage.BeginInit();
bitmapImage.StreamSource = memory;
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
bitmapImage.EndInit();
return bitmapImage;
}
}
}
catch (Exception)
{
return null;
}
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
}