This repository has been archived on 2024-06-04. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
thesis-src/Implementation/GUI/Converters/ImageConverter.cs
T
2024-01-08 16:23:24 +01:00

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();
}
}