Skip to content

Commit 3259c31

Browse files
authored
Merge pull request #16 from DarthAffe/Example
Updated example, to v2.0.0; added Image2Image to example
2 parents f5fc37c + 743f7d4 commit 3259c31

File tree

5 files changed

+120
-121
lines changed

5 files changed

+120
-121
lines changed

Examples/ImageCreationUI/Converter/ImageToImageSourceConverter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
using System.Windows.Data;
55
using System.Windows.Media;
66
using System.Windows.Media.Imaging;
7-
using StableDiffusion.NET.Helper.Images;
7+
using HPPH;
8+
using HPPH.System.Drawing;
89

910
namespace ImageCreationUI.Converter;
1011

@@ -13,7 +14,7 @@ public class ImageToImageSourceConverter : IValueConverter
1314
{
1415
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
1516
{
16-
Bitmap? bitmap = (value as IImage)?.ToBitmap();
17+
using Bitmap? bitmap = (value as IImage)?.ToBitmap();
1718
if (bitmap == null) return null;
1819

1920
using MemoryStream ms = new();

Examples/ImageCreationUI/Extensions/ImageExtension.cs

Lines changed: 0 additions & 77 deletions
This file was deleted.

Examples/ImageCreationUI/ImageCreationUI.csproj

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="StableDiffusion.NET.Backend.Cpu" Version="1.2.0" />
14-
<PackageReference Include="StableDiffusion.NET.Backend.Cuda" Version="1.2.0" />
15-
<PackageReference Include="StableDiffusion.NET.Backend.Rocm" Version="1.2.0" />
16-
<PackageReference Include="StableDiffusion.NET" Version="1.2.0" />
17-
<PackageReference Include="System.Drawing.Common" Version="8.0.4" />
13+
<PackageReference Include="HPPH.System.Drawing" Version="1.0.0" />
14+
<PackageReference Include="StableDiffusion.NET" Version="2.0.0" />
15+
<PackageReference Include="StableDiffusion.NET.Backend.Cpu" Version="2.0.0" />
16+
<PackageReference Include="StableDiffusion.NET.Backend.Cuda" Version="2.0.0" />
17+
<PackageReference Include="StableDiffusion.NET.Backend.Rocm" Version="2.0.0" />
1818
</ItemGroup>
1919

2020
</Project>

Examples/ImageCreationUI/MainWindow.xaml

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,44 @@
7272
<Label Content="AntiPrompt" />
7373
<TextBox Height="80" TextWrapping="Wrap" Text="{Binding AntiPrompt}" />
7474

75-
<StackPanel Orientation="Horizontal" Margin="0,8,0,0">
76-
<Label Width="50" Content="Width" />
77-
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Width}" />
78-
</StackPanel>
79-
80-
<StackPanel Orientation="Horizontal" Margin="0,8,0,0">
81-
<Label Width="50" Content="Height" />
82-
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Height}" />
83-
</StackPanel>
84-
85-
<StackPanel Orientation="Horizontal" Margin="0,8,0,0">
86-
<Label Width="50" Content="Cfg" />
87-
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Cfg}" />
88-
</StackPanel>
89-
90-
<StackPanel Orientation="Horizontal" Margin="0,8,0,0">
91-
<Label Width="50" Content="Steps" />
92-
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Steps}" />
93-
</StackPanel>
94-
95-
<StackPanel Orientation="Horizontal" Margin="0,8,0,0">
96-
<Label Width="50" Content="Seed" />
97-
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Seed}" />
98-
</StackPanel>
75+
<DockPanel LastChildFill="True">
76+
<StackPanel DockPanel.Dock="Left">
77+
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,8,0,0">
78+
<Label Width="50" Content="Width" />
79+
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Width}" />
80+
</StackPanel>
81+
82+
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,8,0,0">
83+
<Label Width="50" Content="Height" />
84+
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Height}" />
85+
</StackPanel>
86+
87+
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,8,0,0">
88+
<Label Width="50" Content="Cfg" />
89+
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Cfg}" />
90+
</StackPanel>
91+
92+
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,8,0,0">
93+
<Label Width="50" Content="Steps" />
94+
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Steps}" />
95+
</StackPanel>
96+
97+
<StackPanel DockPanel.Dock="Top" Orientation="Horizontal" Margin="0,8,0,0">
98+
<Label Width="50" Content="Seed" />
99+
<TextBox HorizontalAlignment="Left" Width="60" Text="{Binding Seed}" />
100+
</StackPanel>
101+
</StackPanel>
102+
103+
<DockPanel Margin="16,0,0,0" LastChildFill="True">
104+
<Label DockPanel.Dock="Top" Content="Image2Image Source" />
105+
<DockPanel DockPanel.Dock="Top" >
106+
<Button DockPanel.Dock="Right" Width="24" Margin="2,0,0,0" Content="..." Command="{Binding SelectImage2ImageSourceCommand}" IsEnabled="{Binding IsReady}" />
107+
<TextBox Text="{Binding Image2ImageSourcePath}" />
108+
</DockPanel>
109+
110+
<Image Source="{Binding Image2ImageSource, Converter={StaticResource ImageToImageSourceConverter}}" />
111+
</DockPanel>
112+
</DockPanel>
99113

100114
<Label Content="Sample-Method" />
101115
<ComboBox ItemsSource="{Binding Source={StaticResource SamplerDataSource}}" SelectedItem="{Binding SampleMethod}" />

Examples/ImageCreationUI/MainWindowViewModel.cs

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
using System.ComponentModel;
2+
using System.Drawing.Imaging;
23
using System.IO;
34
using System.Runtime.CompilerServices;
5+
using HPPH;
6+
using HPPH.System.Drawing;
47
using Microsoft.Win32;
58
using StableDiffusion.NET;
6-
using StableDiffusion.NET.Helper.Images;
79

810
namespace ImageCreationUI;
911

@@ -90,6 +92,33 @@ public Sampler SampleMethod
9092
set => SetProperty(ref _sampleMethod, value);
9193
}
9294

95+
private string _image2ImageSourcePath = string.Empty;
96+
public string Image2ImageSourcePath
97+
{
98+
get => _image2ImageSourcePath;
99+
set
100+
{
101+
if (SetProperty(ref _image2ImageSourcePath, value))
102+
{
103+
try
104+
{
105+
Image2ImageSource = ImageHelper.LoadImage(value).ConvertTo<ColorRGB>();
106+
}
107+
catch
108+
{
109+
Image2ImageSource = null;
110+
}
111+
}
112+
}
113+
}
114+
115+
private IImage<ColorRGB>? _image2ImageSource;
116+
public IImage<ColorRGB>? Image2ImageSource
117+
{
118+
get => _image2ImageSource;
119+
set => SetProperty(ref _image2ImageSource, value);
120+
}
121+
93122
private IImage? _image;
94123
public IImage? Image
95124
{
@@ -130,6 +159,9 @@ public bool IsReady
130159
private ActionCommand? _selectVaeCommand;
131160
public ActionCommand SelectVaeCommand => _selectVaeCommand ??= new ActionCommand(SelectVae);
132161

162+
private ActionCommand? _selectImage2ImageSourceCommand;
163+
public ActionCommand SelectImage2ImageSourceCommand => _selectImage2ImageSourceCommand ??= new ActionCommand(SelectImage2ImageSource);
164+
133165
#endregion
134166

135167
#region Constructors
@@ -178,19 +210,35 @@ private async void CreateImage()
178210
{
179211
IsReady = false;
180212

181-
LogLine("Creating image ...");
182-
using StableDiffusionImage? image = await Task.Run(() => _model?.TextToImage(Prompt, new StableDiffusionParameter
213+
if (Image2ImageSource == null)
214+
{
215+
LogLine("Creating image ...");
216+
Image = await Task.Run(() => _model?.TextToImage(Prompt, new StableDiffusionParameter
217+
{
218+
NegativePrompt = AntiPrompt,
219+
Width = Width,
220+
Height = Height,
221+
CfgScale = Cfg,
222+
SampleSteps = Steps,
223+
Seed = Seed,
224+
SampleMethod = SampleMethod
225+
}));
226+
}
227+
else
183228
{
184-
NegativePrompt = AntiPrompt,
185-
Width = Width,
186-
Height = Height,
187-
CfgScale = Cfg,
188-
SampleSteps = Steps,
189-
Seed = Seed,
190-
SampleMethod = SampleMethod
191-
}));
192-
193-
Image = image?.ToImage();
229+
LogLine("Manipulating image ...");
230+
Image = await Task.Run(() => _model?.ImageToImage(Prompt, Image2ImageSource, new StableDiffusionParameter
231+
{
232+
NegativePrompt = AntiPrompt,
233+
Width = Width,
234+
Height = Height,
235+
CfgScale = Cfg,
236+
SampleSteps = Steps,
237+
Seed = Seed,
238+
SampleMethod = SampleMethod
239+
}));
240+
}
241+
194242
LogLine("done!");
195243
}
196244
catch (Exception ex)
@@ -236,6 +284,19 @@ private void SelectVae()
236284
VaePath = openFileDialog.FileName;
237285
}
238286

287+
private void SelectImage2ImageSource()
288+
{
289+
IEnumerable<string> codecs = ["All Files (*.*)|*.*", .. ImageCodecInfo.GetImageDecoders().Select(c =>
290+
{
291+
string codecName = c.CodecName![8..].Replace("Codec", "Files").Trim();
292+
return $"{codecName} ({c.FilenameExtension})|{c.FilenameExtension}";
293+
})];
294+
295+
OpenFileDialog openFileDialog = new() { Filter = string.Join('|', codecs) };
296+
if (openFileDialog.ShowDialog() == true)
297+
Image2ImageSourcePath = openFileDialog.FileName;
298+
}
299+
239300
private void LogLine(string line, bool appendNewLine = true)
240301
{
241302
if (appendNewLine)

0 commit comments

Comments
 (0)