Skip to content

StabilityAI

Nuget package dotnet License: MIT Discord

Features 🔥

  • Fully generated C# SDK based on official Stability AI OpenAPI specification using AutoSDK
  • Same day update to support new features
  • Updated and supported automatically if there are no breaking changes
  • All modern .NET features - nullability, trimming, NativeAOT, etc.
  • Support .Net Framework/.Net Standard 2.0

Usage

1
2
3
using StabilityAI;

using var client = new StabilityAIClient(apiKey);

List Engines

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
using var client = new StabilityAIClient(apiKey);

// Discover available engines before generating images
var engines = await client.V1Engines.ListEnginesAsync();

foreach (var engine in engines)
{
    Console.WriteLine($"{engine.Id}: {engine.Name} ({engine.Type})");
    Console.WriteLine($"  {engine.Description}");
}

Text to Image

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using var client = new StabilityAIClient(apiKey);

var images = await client.V1Generation.TextToImageAsync(
    engineId: "stable-diffusion-v1-6",
    request: new TextToImageRequestBody(
        value1: new TextToImageRequestBodyVariant1
        {
            TextPrompts =
            [
                new TextPrompt
                {
                    Text = "A beautiful sunset over mountains, digital art",
                    Weight = 1.0f,
                },
            ],
            Height = 512,
            Width = 512,
        },
        value2: new GenerationRequestOptionalParams
        {
            Samples = 1,
        }));

foreach (var image in images)
{
    Console.WriteLine($"Seed: {image.Seed}, Finish reason: {image.FinishReason}");

    // image.Base64 contains the generated image data
    var bytes = Convert.FromBase64String(image.Base64!);
    await File.WriteAllBytesAsync($"output_{image.Seed}.png", bytes);
}

Image to Image

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
using var client = new StabilityAIClient(apiKey);

// Load the source image
var initImageBytes = await File.ReadAllBytesAsync("input.png");

var images = await client.V1Generation.ImageToImageAsync(
    engineId: "stable-diffusion-v1-6",
    request: new ImageToImageRequestBody
    {
        TextPrompts =
        [
            new TextPrompt
            {
                Text = "A fantasy castle on a cliff, dramatic lighting",
                Weight = 1.0f,
            },
        ],
        InitImage = initImageBytes,
        InitImagename = "input.png",
        ImageStrength = 0.35f, // Lower = closer to original, higher = more creative
    });

foreach (var image in images)
{
    Console.WriteLine($"Seed: {image.Seed}, Finish reason: {image.FinishReason}");

    var bytes = Convert.FromBase64String(image.Base64!);
    await File.WriteAllBytesAsync($"output_{image.Seed}.png", bytes);
}

Upscale Image

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
using var client = new StabilityAIClient(apiKey);

// Load the image to upscale
var imageBytes = await File.ReadAllBytesAsync("low-res.png");

var images = await client.V1Generation.UpscaleImageAsync(
    engineId: "esrgan-v1-x2plus",
    request: new UpscaleImageRequestBody
    {
        Image = imageBytes,
        Imagename = "low-res.png",
        Width = 2048, // Only specify width OR height, not both
    });

foreach (var image in images)
{
    Console.WriteLine($"Seed: {image.Seed}, Finish reason: {image.FinishReason}");

    var bytes = Convert.FromBase64String(image.Base64!);
    await File.WriteAllBytesAsync("upscaled.png", bytes);
}

Masking

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using var client = new StabilityAIClient(apiKey);

// Load the source image and mask image (must be same dimensions)
var initImageBytes = await File.ReadAllBytesAsync("input.png");
var maskImageBytes = await File.ReadAllBytesAsync("mask.png");

var images = await client.V1Generation.MaskingAsync(
    engineId: "stable-diffusion-v1-6",
    request: new MaskingRequestBody
    {
        TextPrompts =
        [
            new TextPrompt
            {
                Text = "A bright blue sky with fluffy clouds",
                Weight = 1.0f,
            },
        ],
        InitImage = initImageBytes,
        InitImagename = "input.png",
        MaskImage = maskImageBytes,
        MaskImagename = "mask.png",
    });

foreach (var image in images)
{
    Console.WriteLine($"Seed: {image.Seed}, Finish reason: {image.FinishReason}");

    var bytes = Convert.FromBase64String(image.Base64!);
    await File.WriteAllBytesAsync($"masked_{image.Seed}.png", bytes);
}

Support

Priority place for bugs: https://github.com/tryAGI/StabilityAI/issues
Priority place for ideas and general questions: https://github.com/tryAGI/StabilityAI/discussions
Discord: https://discord.gg/Ca2xhfBf3v

Acknowledgments

JetBrains logo

This project is supported by JetBrains through the Open Source Support Program.