Core .NET client library for Stable Diffusion WebUI (AUTOMATIC1111) API without dependency injection. Lightweight package with minimal dependencies. Supports Text-to-Image, Image-to-Image generation, model management, progress tracking, and more. Compatible with .NET Standard 2.0+, .NET Framework 4.7.2+, .NET Core 2.0+, .NET 5.0+.
$ dotnet add package StableDiffusionNet.CoreEnglish | Русский
Core library for working with Stable Diffusion WebUI API.
dotnet add package StableDiffusionNet.Core
Or via NuGet Package Manager:
Install-Package StableDiffusionNet.Core
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 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();
// 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();
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");
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);
// 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");
var progress = await client.Progress.GetProgressAsync();
Console.WriteLine($"Progress: {progress.Progress:P}");
The library includes a reliable custom retry implementation with exponential backoff:
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}");
}
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";
});
MIT License