Skip to content

Masking

This example assumes using StabilityAI; is in scope and apiKey contains your StabilityAI API key.

 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);
}