From a19812896742a5036979b28d7555a28e76bb6d38 Mon Sep 17 00:00:00 2001 From: Darth Affe Date: Fri, 22 Mar 2024 23:45:41 +0100 Subject: [PATCH] Added example application --- ImageCreationUI/ActionCommand.cs | 42 ++++ ImageCreationUI/App.xaml | 4 + ImageCreationUI/App.xaml.cs | 14 ++ .../Converter/ImageToImageSourceConverter.cs | 33 +++ ImageCreationUI/Extensions/ImageExtension.cs | 45 ++++ ImageCreationUI/ImageCreationUI.csproj | 20 ++ .../ImageCreationUI.csproj.DotSettings | 2 + ImageCreationUI/MainWindow.xaml | 85 +++++++ ImageCreationUI/MainWindow.xaml.cs | 11 + ImageCreationUI/MainWindowViewModel.cs | 225 ++++++++++++++++++ StableDiffusion.NET.sln | 13 +- 11 files changed, 493 insertions(+), 1 deletion(-) create mode 100644 ImageCreationUI/ActionCommand.cs create mode 100644 ImageCreationUI/App.xaml create mode 100644 ImageCreationUI/App.xaml.cs create mode 100644 ImageCreationUI/Converter/ImageToImageSourceConverter.cs create mode 100644 ImageCreationUI/Extensions/ImageExtension.cs create mode 100644 ImageCreationUI/ImageCreationUI.csproj create mode 100644 ImageCreationUI/ImageCreationUI.csproj.DotSettings create mode 100644 ImageCreationUI/MainWindow.xaml create mode 100644 ImageCreationUI/MainWindow.xaml.cs create mode 100644 ImageCreationUI/MainWindowViewModel.cs diff --git a/ImageCreationUI/ActionCommand.cs b/ImageCreationUI/ActionCommand.cs new file mode 100644 index 0000000..2b87fdb --- /dev/null +++ b/ImageCreationUI/ActionCommand.cs @@ -0,0 +1,42 @@ +using System.Windows.Input; + +namespace ImageCreationUI; + +public class ActionCommand(Action command, Func? canExecute = null) : ICommand +{ + #region Events + + public event EventHandler? CanExecuteChanged; + + #endregion + + #region Methods + + public bool CanExecute(object? parameter) => canExecute?.Invoke() ?? true; + + public void Execute(object? parameter) => command.Invoke(); + + public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty); + + #endregion +} + +public class ActionCommand(Action command, Func? canExecute = null) : ICommand + where T : class +{ + #region Events + + public event EventHandler? CanExecuteChanged; + + #endregion + + #region Methods + + public bool CanExecute(object? parameter) => canExecute?.Invoke((T)parameter!) ?? true; + + public void Execute(object? parameter) => command.Invoke((T)parameter!); + + public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty); + + #endregion +} \ No newline at end of file diff --git a/ImageCreationUI/App.xaml b/ImageCreationUI/App.xaml new file mode 100644 index 0000000..84ead37 --- /dev/null +++ b/ImageCreationUI/App.xaml @@ -0,0 +1,4 @@ + diff --git a/ImageCreationUI/App.xaml.cs b/ImageCreationUI/App.xaml.cs new file mode 100644 index 0000000..d92161c --- /dev/null +++ b/ImageCreationUI/App.xaml.cs @@ -0,0 +1,14 @@ +using System.Configuration; +using System.Data; +using System.Windows; + +namespace ImageCreationUI +{ + /// + /// Interaction logic for App.xaml + /// + public partial class App : Application + { + } + +} diff --git a/ImageCreationUI/Converter/ImageToImageSourceConverter.cs b/ImageCreationUI/Converter/ImageToImageSourceConverter.cs new file mode 100644 index 0000000..6370177 --- /dev/null +++ b/ImageCreationUI/Converter/ImageToImageSourceConverter.cs @@ -0,0 +1,33 @@ +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(); +} \ No newline at end of file diff --git a/ImageCreationUI/Extensions/ImageExtension.cs b/ImageCreationUI/Extensions/ImageExtension.cs new file mode 100644 index 0000000..dbc7e3e --- /dev/null +++ b/ImageCreationUI/Extensions/ImageExtension.cs @@ -0,0 +1,45 @@ +using System.Drawing; +using System.Drawing.Imaging; +using StableDiffusion.NET.Helper.Images.Colors; +using StableDiffusion.NET.Helper.Images; +using System.IO; + +namespace ImageCreationUI; + +public static class ImageExtension +{ + public static Bitmap ToBitmap(this IImage image) => image.AsRefImage().ToBitmap(); + public static Bitmap ToBitmap(this Image image) => image.AsRefImage().ToBitmap(); + + public static unsafe Bitmap ToBitmap(this RefImage image) + { + Bitmap output = new(image.Width, image.Height, PixelFormat.Format24bppRgb); + Rectangle rect = new(0, 0, image.Width, image.Height); + BitmapData bmpData = output.LockBits(rect, ImageLockMode.ReadWrite, output.PixelFormat); + + nint ptr = bmpData.Scan0; + foreach (ReadOnlyRefEnumerable row in image.Rows) + { + Span target = new((void*)ptr, bmpData.Stride); + for (int i = 0; i < row.Length; i++) + { + ColorRGB srcColor = row[i]; + target[i] = new ColorBGR(srcColor.B, srcColor.G, srcColor.R); + } + + ptr += bmpData.Stride; + } + + output.UnlockBits(bmpData); + return output; + } + + public static byte[] ToPng(this IImage image) + { + using Bitmap bitmap = image.ToBitmap(); + using MemoryStream ms = new(); + bitmap.Save(ms, ImageFormat.Png); + + return ms.ToArray(); + } +} \ No newline at end of file diff --git a/ImageCreationUI/ImageCreationUI.csproj b/ImageCreationUI/ImageCreationUI.csproj new file mode 100644 index 0000000..f5e25c8 --- /dev/null +++ b/ImageCreationUI/ImageCreationUI.csproj @@ -0,0 +1,20 @@ + + + + WinExe + net8.0-windows + enable + enable + true + true + + + + + + + + + + + diff --git a/ImageCreationUI/ImageCreationUI.csproj.DotSettings b/ImageCreationUI/ImageCreationUI.csproj.DotSettings new file mode 100644 index 0000000..17962b1 --- /dev/null +++ b/ImageCreationUI/ImageCreationUI.csproj.DotSettings @@ -0,0 +1,2 @@ + + True \ No newline at end of file diff --git a/ImageCreationUI/MainWindow.xaml b/ImageCreationUI/MainWindow.xaml new file mode 100644 index 0000000..0e85d90 --- /dev/null +++ b/ImageCreationUI/MainWindow.xaml @@ -0,0 +1,85 @@ + + + + + + + + + + + + + + + + + + + + + + + +