Skip to content

IImageGenerator

Key Points

  • IImageGenerator abstracts image generation (DALL-E / GPT-Image / Stable Diffusion / FLUX).
  • Vendor-neutral; same pipeline pattern.
  • For most apps: OpenAI GPT-Image or Stable Diffusion via Replicate / self-hosted.
  • Use cases: thumbnails, marketing visuals, document illustrations, content moderation testing.

Setup

IImageGenerator gen = new OpenAIClient(apiKey).AsImageGenerator("dall-e-3");

// or Azure OpenAI
IImageGenerator azureGen = new AzureOpenAIClient(uri, cred).AsImageGenerator(deploymentName);

Generate

var result = await gen.GenerateAsync("A futuristic city at sunset");
foreach (var image in result.Images)
{
    var bytes = image.Data.ToArray();   // or image.Uri
    File.WriteAllBytes("out.png", bytes);
}

Options

new ImageGenerationOptions
{
    Size = ImageSize.Square1024,    // 1024x1024
    Quality = ImageQuality.Standard, // or HD
    Style = ImageStyle.Vivid,
    Count = 1,
    ResponseFormat = ImageResponseFormat.Bytes  // or Uri
}

Pipeline

gen = gen.AsBuilder()
    .UseLogging(loggerFactory)
    .UseOpenTelemetry()
    .Build();

Less middleware than chat — no function calling for image gen.

Cost

DALL-E 3 / GPT-Image: - Standard 1024×1024: ~$0.04/image - HD 1024×1024: ~$0.08/image

Stable Diffusion via Replicate / self-host: cheaper, lower-quality (older versions) or comparable (FLUX).

Stable Diffusion / FLUX

For self-hosted or replicate:

// Replicate (community .NET SDK or HTTP)
var resp = await replicate.Run("stability-ai/sdxl", new { prompt = "..." });

For ComfyUI / Automatic1111: HTTP API. Wrap in custom IImageGenerator adapter.

Use cases

  • Marketing visuals: blog thumbnails, social posts.
  • Product mockups: variations from a base image.
  • Avatars: user-generated.
  • Documentation: explanatory diagrams.
  • Game assets: concept art (not final).

Restrictions

  • Content filters (no violence, sexual content, copyrighted characters).
  • Vendor varies in strictness.
  • DALL-E enforces; some open models don't.

Image variations

OpenAI GPT-Image supports edit / variations:

// Edit (mask + prompt)
var resp = await gen.EditImageAsync(originalBytes, maskBytes, "Replace the cat with a dog");

// Variations
var resp = await gen.CreateVariationsAsync(originalBytes, count: 3);

Vision (analyzing images) is different

Vision is IChatClient with DataContent for images (input). IImageGenerator is for output.

Senior considerations

  • Content moderation: review user prompts to avoid policy violations.
  • Rate limits: image gen often slower (10-60s); poll or async pattern.
  • Caching: identical prompt → cache result.
  • Cost monitoring: easy to runaway.
  • Storage: generated images stored in Blob; CDN-served.

Cross-references