49 lines
1.1 KiB
C#
49 lines
1.1 KiB
C#
using ImageMagick;
|
|
using Serilog;
|
|
using System;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Windows.Data;
|
|
using System.Windows.Media.Imaging;
|
|
|
|
namespace Ocr.Gui.Converters;
|
|
|
|
internal class ImageConverter : IValueConverter
|
|
{
|
|
/// <inheritdoc />
|
|
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
if (value is not MagickImage image)
|
|
{
|
|
return Binding.DoNothing;
|
|
}
|
|
|
|
try
|
|
{
|
|
using var stream = new MemoryStream();
|
|
|
|
// Save image to stream
|
|
image.Write(stream, MagickFormat.Png);
|
|
|
|
// Build Bitmap from stream
|
|
var imageSource = new BitmapImage();
|
|
imageSource.BeginInit();
|
|
imageSource.StreamSource = stream;
|
|
imageSource.CacheOption = BitmapCacheOption.OnLoad;
|
|
imageSource.EndInit();
|
|
|
|
return imageSource;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Log.Error($"{e.Message}");
|
|
return Binding.DoNothing;
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
} |