StableDiffusionNet.Core
1.1.5
dotnet add package StableDiffusionNet.Core --version 1.1.5
NuGet\Install-Package StableDiffusionNet.Core -Version 1.1.5
<PackageReference Include="StableDiffusionNet.Core" Version="1.1.5" />
<PackageVersion Include="StableDiffusionNet.Core" Version="1.1.5" />
<PackageReference Include="StableDiffusionNet.Core" />
paket add StableDiffusionNet.Core --version 1.1.5
#r "nuget: StableDiffusionNet.Core, 1.1.5"
#:package StableDiffusionNet.Core@1.1.5
#addin nuget:?package=StableDiffusionNet.Core&version=1.1.5
#tool nuget:?package=StableDiffusionNet.Core&version=1.1.5
StableDiffusionNet.Core
English | Русский
Core library for working with Stable Diffusion WebUI API.
Features
- Minimal dependencies: only Newtonsoft.Json for serialization
- No DI: simple to use without DI infrastructure
- Built-in Retry: custom retry logic implementation
- Asynchronous operations: async/await and CancellationToken support
- XML documentation: for all public APIs
- Builder Pattern: convenient client creation
- Custom logging: minimalistic abstraction without Microsoft.Extensions
- Multi-targeting: supports .NET Standard 2.0, 2.1, .NET 6.0, .NET 8.0
Installation
dotnet add package StableDiffusionNet.Core
Or via NuGet Package Manager:
Install-Package StableDiffusionNet.Core
Quick Start
Simplest Option
using StableDiffusionNet;
using StableDiffusionNet.Models.Requests;
// Create client with default settings
var client = StableDiffusionClientBuilder.CreateDefault("http://localhost:7860");
// Generate image
var request = new TextToImageRequest
{
Prompt = "a beautiful sunset over mountains, highly detailed, 4k",
NegativePrompt = "blurry, low quality",
Width = 512,
Height = 512,
Steps = 30
};
var response = await client.TextToImage.GenerateAsync(request);
Using Builder for Configuration
using StableDiffusionNet;
using StableDiffusionNet.Logging;
// Create client with additional settings
var client = new StableDiffusionClientBuilder()
.WithBaseUrl("http://localhost:7860")
.WithTimeout(600)
.WithRetry(retryCount: 3, retryDelayMilliseconds: 1000)
.WithApiKey("your-api-key-if-needed")
.WithDetailedLogging()
.Build();
Using with Custom Logging
// Implement IStableDiffusionLogger
public class ConsoleLogger : IStableDiffusionLogger
{
public void Log(LogLevel logLevel, string message)
{
Console.WriteLine($"[{logLevel}] {message}");
}
public void Log(LogLevel logLevel, Exception exception, string message)
{
Console.WriteLine($"[{logLevel}] {message}: {exception}");
}
public bool IsEnabled(LogLevel logLevel) => true;
}
// Implement IStableDiffusionLoggerFactory
public class ConsoleLoggerFactory : IStableDiffusionLoggerFactory
{
public IStableDiffusionLogger CreateLogger<T>() => new ConsoleLogger();
public IStableDiffusionLogger CreateLogger(string categoryName) => new ConsoleLogger();
}
// Use with Builder
var client = new StableDiffusionClientBuilder()
.WithBaseUrl("http://localhost:7860")
.WithLoggerFactory(new ConsoleLoggerFactory())
.Build();
Main Features
Text-to-Image Generation
var request = new TextToImageRequest
{
Prompt = "a cute cat, highly detailed",
Width = 512,
Height = 512,
Steps = 20,
CfgScale = 7.5
};
var response = await client.TextToImage.GenerateAsync(request);
ImageHelper.Base64ToImage(response.Images[0], "output.png");
Image-to-Image Generation
var initImage = ImageHelper.ImageToBase64("input.png");
var request = new ImageToImageRequest
{
InitImages = new List<string> { initImage },
Prompt = "transform into van gogh style",
DenoisingStrength = 0.7
};
var response = await client.ImageToImage.GenerateAsync(request);
Working with Models
// Get list of models
var models = await client.Models.GetModelsAsync();
// Get current model
var currentModel = await client.Models.GetCurrentModelAsync();
// Set model
await client.Models.SetModelAsync("sd_xl_base_1.0.safetensors");
Monitoring Progress
var progress = await client.Progress.GetProgressAsync();
Console.WriteLine($"Progress: {progress.Progress:P}");
Retry Logic
The library includes a reliable custom retry implementation with exponential backoff:
- Automatic retries for transient errors (500, 502, 503, 504)
- Special handling for rate limiting (HTTP 429)
- Exponential backoff with jitter to avoid thundering herd
- Configurable retry count and delays
Error Handling
using StableDiffusionNet.Exceptions;
try
{
var response = await client.TextToImage.GenerateAsync(request);
}
catch (ApiException ex)
{
Console.WriteLine($"API Error: {ex.Message}");
Console.WriteLine($"Status Code: {ex.StatusCode}");
Console.WriteLine($"Response: {ex.ResponseBody}");
}
catch (ConfigurationException ex)
{
Console.WriteLine($"Configuration Error: {ex.Message}");
}
catch (StableDiffusionException ex)
{
Console.WriteLine($"General Error: {ex.Message}");
}
Need Dependency Injection?
If you need integration with Microsoft.Extensions.DependencyInjection, use the StableDiffusionNet.DependencyInjection package:
dotnet add package StableDiffusionNet.DependencyInjection
services.AddStableDiffusion(options =>
{
options.BaseUrl = "http://localhost:7860";
});
License
MIT License
Links
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Newtonsoft.Json (>= 13.0.4)
-
.NETStandard 2.1
- Newtonsoft.Json (>= 13.0.4)
-
net6.0
- Newtonsoft.Json (>= 13.0.4)
-
net8.0
- Newtonsoft.Json (>= 13.0.4)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on StableDiffusionNet.Core:
| Package | Downloads |
|---|---|
|
StableDiffusionNet.DependencyInjection
Dependency Injection extensions for StableDiffusionNet.Core. Provides Microsoft.Extensions.DependencyInjection integration, logging adapters, and IOptions pattern support for Stable Diffusion WebUI API client. Compatible with .NET Standard 2.0+, .NET Framework 4.7.2+, .NET Core 2.0+, .NET 5.0+. |
GitHub repositories
This package is not used by any popular GitHub repositories.
See CHANGELOG.md for details