⚠️ This blog post was created with the help of AI tools. Yes, I used a bit of magic from language models to organize my thoughts and automate the boring parts, but the geeky fun and the 🤖 in C# are 100% mine.

Hi!

When Microsoft announced MAI-Image-2, I immediately thought: “I need to add this to ElBruno.Text2Image. Today.”

So I did. 😄

MAI-Image-2 is Microsoft’s new image generation model on Microsoft Foundry — high-quality generation, a synchronous API (no polling!), a 32K character prompt limit, and flexible dimensions. And it’s already supported in ElBruno.Text2Image with the same clean interface you already know.

Let me show you how it works.


☁️ Getting Started — MAI-Image-2 on Azure AI Foundry

MAI-Image-2 delivers high-quality image generation with a simpler developer experience than FLUX.2. The API is synchronous — you send a request, you get an image back. No 202 status codes, no polling loops, no waiting callbacks. Just a prompt and a picture.

Here’s all you need:

using ElBruno.Text2Image;
using ElBruno.Text2Image.Foundry;
using var generator = new MaiImage2Generator(
endpoint: "https://your-resource.services.ai.azure.com",
apiKey: "your-api-key",
modelId: "MAI-Image-2");
var result = await generator.GenerateAsync(
"a futuristic cityscape with neon lights, cyberpunk style");
await result.SaveAsync("mai-image2-output.png");
Console.WriteLine($"Generated in {result.InferenceTimeMs}ms");

Setting up credentials

The library reads from User Secrets, environment variables, or appsettings.json. For local development:

dotnet user-secrets set MAI_IMAGE2_ENDPOINT "https://your-resource.services.ai.azure.com"
dotnet user-secrets set MAI_IMAGE2_API_KEY "your-api-key-here"
dotnet user-secrets set MAI_IMAGE2_MODEL_ID "MAI-Image-2"

💡 Fun fact: MAI-Image-2 uses a dedicated /mai/v1/images/generations endpoint. The library handles this automatically — just provide your .services.ai.azure.com base URL and it builds the correct API path for you.


⚡ Key Differences from FLUX.2

If you’re already using FLUX.2 with this library, here’s how MAI-Image-2 compares:

FeatureMAI-Image-2FLUX.2
API styleSynchronous (direct response)Asynchronous (202 + polling)
API path/mai/v1/images/generationsBFL provider path
Prompt limit32,000 characters~1,000 characters
Min dimensions768px (per side)256px
Max dimensions1M total pixelsModel-dependent
InterfaceIImageGeneratorIImageGenerator
DI support✅ Same pattern✅ Same pattern
Endpoint auto-conversion

The synchronous API is a big deal for developer experience. No more writing polling loops or handling intermediate states. Send a prompt, get an image. Done.


🔌 Same Interface, Multiple Backends

🧩 Microsoft.Extensions.AI Compatible

Every generator in the library — including the new MaiImage2Generator — implements the standard Microsoft.Extensions.AI.IImageGenerator interface from the
Microsoft.Extensions.AI.Abstractions package. This means you can use ElBruno.Text2Image as a drop-in provider anywhere the MEAI abstraction is expected — dependency
injection, middleware pipelines, or any framework that programs against IImageGenerator. Cloud or local, FLUX.2 or MAI-Image-2 or Stable Diffusion — they all plug
into the same standard .NET AI contract.

// MAI-Image-2 (cloud)
IImageGenerator generator = new MaiImage2Generator(endpoint, apiKey, modelId: "MAI-Image-2");
// FLUX.2 Pro (cloud)
IImageGenerator generator = new Flux2Generator(endpoint, apiKey, modelId: "FLUX.2-pro");
// Stable Diffusion 1.5 (local)
IImageGenerator generator = new StableDiffusion15();
// Same API for all three
var result = await generator.GenerateAsync("a beautiful landscape");

💉 Dependency Injection

If you’re building with DI, the library has an extension method ready to go:

services.AddMaiImage2Generator(
endpoint: "https://your-resource.services.ai.azure.com",
apiKey: "your-api-key",
modelId: "MAI-Image-2");

Same pattern as the FLUX.2 registration. Inject IImageGenerator and you’re done.


🔗 Links

Happy coding!

Greetings

El Bruno

More posts in my blog ElBruno.com.

More info in https://beacons.ai/elbruno


Leave a comment

Discover more from El Bruno

Subscribe now to keep reading and get access to the full archive.

Continue reading