mirror of
https://github.com/DarthAffe/StableDiffusion.NET.git
synced 2025-12-13 05:48:40 +00:00
33 lines
1.0 KiB
C#
33 lines
1.0 KiB
C#
using System.Drawing;
|
|
using System.Globalization;
|
|
using System.IO;
|
|
using System.Windows.Data;
|
|
using System.Windows.Media;
|
|
using System.Windows.Media.Imaging;
|
|
using StableDiffusion.NET.Helper.Images;
|
|
|
|
namespace ImageCreationUI.Converter;
|
|
|
|
[ValueConversion(typeof(IImage), typeof(ImageSource))]
|
|
public class ImageToImageSourceConverter : IValueConverter
|
|
{
|
|
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
|
|
{
|
|
Bitmap? bitmap = (value as IImage)?.ToBitmap();
|
|
if (bitmap == null) return null;
|
|
|
|
using MemoryStream ms = new();
|
|
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
|
|
ms.Position = 0;
|
|
|
|
BitmapImage bitmapImage = new();
|
|
bitmapImage.BeginInit();
|
|
bitmapImage.StreamSource = ms;
|
|
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
|
|
bitmapImage.EndInit();
|
|
|
|
return bitmapImage;
|
|
}
|
|
|
|
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) => throw new NotSupportedException();
|
|
} |