> ## Content Index
> Fetch the complete content index at: https://codetraveler.io/llms.txt
> Use this file to discover other available public pages before exploring further.

# Generating Images in .NET with `IImageGenerator`
- URL: https://codetraveler.io/2026/07/27/generating-images-in-net-with-iimagegenerator/
- Published: 2026-07-27T09:00:48.000Z
- Updated: 2026-07-27T09:00:48.000Z
- Author: Brandon Minnick

Did you know [Microsoft.Extensions.AI](https://www.nuget.org/packages/Microsoft.Extensions.AI?ref=codetraveler.io) can generate images too? `IImageGenerator` makes it easy!

This is Part 3 of our `Microsoft.Extensions.AI` series. In [Part 1](https://codetraveler.io/2026/07/16/adding-ai-to-your-net-app-with-ichatclient/) we created an AI Chat Bot with an LLM using `IChatClient`, and in [Part 2](https://codetraveler.io/2026/07/20/adding-semantic-search-to-net-apps-with-iembeddinggenerator/) we built semantic search using `IEmbeddingGenerator`. This week, we're generating images!

You're welcome to skip the definitions and jump directly to the code below.

## What is IImageGenerator?

`IImageGenerator` is the newest abstraction in MEAI: a text prompt goes in, an image comes out.

And just like `IChatClient` and `IEmbeddingGenerator`, it's one interface that AI providers plug into, so we can swap image models without rewriting our apps.

## Opt-into this Experimental Feature

The MEAI team released `IImageGenerator` using the `[Experimental]` attribute. This lets the community test its features and provide feedback while giving the .NET team the flexibility to modify the API.

In practice, this means our code won't compile until we opt in!

1. In the `csproj` file of your app, suppress `MEAI001`:

```xml
<!-- Opt into the experimental Microsoft.Extensions.AI image generation APIs -->
<PropertyGroup>
    <NoWarn>$(NoWarn);MEAI001</NoWarn>
</PropertyGroup>

```

> **Important:** `[Experimental]` means the API surface may still change in a future release, so double-check the [release notes](https://github.com/dotnet/extensions/releases?ref=codetraveler.io) when updating the NuGet Package.

## Code

Now that we're opted in, let's write some code!

You can find the [completed code sample here](https://github.com/Dometrain/from-zero-to-hero-microsoft-extensions-ai/tree/main/6.%20Generating%20Images?ref=codetraveler.io).

### Prerequisites

If you'd like to follow along, here are the steps.

Image models are too big to run locally (they are hungry beasts!), so this week we'll use OpenAI instead of Ollama.

1. [Create an OpenAI API Key](https://platform.openai.com/api-keys?ref=codetraveler.io)

Now we can finally write some code!

### Console App

First, let's create a File->New Console app:

1. Create a [File -> New Console App in Visual Studio](https://learn.microsoft.com/dotnet/core/tutorials/with-visual-studio?ref=codetraveler.io)
2. Add the [Microsoft.Extensions.AI](https://www.nuget.org/packages/Microsoft.Extensions.AI?ref=codetraveler.io) and [Microsoft.Extensions.AI.OpenAI](https://www.nuget.org/packages/Microsoft.Extensions.AI.OpenAI?ref=codetraveler.io) NuGet Packages to the app.

> If you're unfamiliar with adding NuGet Packages, you can find more information [here](https://learn.microsoft.com/nuget/consume-packages/install-use-packages-visual-studio?ref=codetraveler.io#find-and-install-a-package).

```csharp
using Microsoft.Extensions.AI;
using OpenAI;
using Size = System.Drawing.Size;

// GetImageClient().AsIImageGenerator() returns IImageGenerator, the abstraction provided by Microsoft.Extensions.AI
IImageGenerator generator = new OpenAIClient(Environment.GetEnvironmentVariable("OPENAI_API_KEY"))
                                .GetImageClient("gpt-image-1.5")
                                .AsIImageGenerator();

ImageGenerationOptions options = new()
{
    MediaType = "image/png",
    ImageSize = new Size(1024, 1024)
};

ImageGenerationResponse response = await generator.GenerateImagesAsync(
    "A watercolor painting of a robot writing C# code on a laptop at the beach",
    options);

// The generated image is returned as DataContent (the raw png bytes)
DataContent? image = response.Contents.OfType<DataContent>().FirstOrDefault();

if (image is not null)
{
    await File.WriteAllBytesAsync("robot.png", image.Data.ToArray());
    Console.WriteLine("Saved robot.png");
}

```

Now let's click **Run** and see what got generated:

![](https://storage.ghost.io/c/dc/c2/dcc2e43e-24f8-46bb-a899-9c446f3ef794/content/images/2026/07/image_c2ed9149-1.png)

It works! One prompt, one method call, one C#-coding robot 🎉

### Editing Images

Here's the best part: `EditImageAsync` takes an existing image plus an instruction, and returns a modified image. No masks, no layers, no Photoshop — just a sentence!

Let's give our robot a party hat:

```csharp
DataContent original = new(await File.ReadAllBytesAsync("robot.png"), "image/png");

ImageGenerationResponse edited = await generator.EditImageAsync(
    original,
    "Add a tiny birthday party hat on the robot's head",
    options);

DataContent? result = edited.Contents.OfType<DataContent>().FirstOrDefault();

if (result is not null)
{
    await File.WriteAllBytesAsync("robot-party.png", result.Data.ToArray());
}

```

![](https://storage.ghost.io/c/dc/c2/dcc2e43e-24f8-46bb-a899-9c446f3ef794/content/images/2026/07/image_86da9ab8.png)

So cool!!! Think about the features this one method unlocks: user avatars with applied styles; product photos with seasonal decorations; a "match our brand colors" button; the possibilities are endless!

## Conclusion

As .NET developers, we can now generate AND edit images in just a few lines of code, thanks to `IImageGenerator`.

And because it's part of Microsoft.Extensions.AI, everything we've already learned still applies: we can register it with Dependency Injection using `AddImageGenerator()`, and add middleware like logging and telemetry — exactly like we do with `IChatClient`.

You can find the completed code sample here: [https://github.com/Dometrain/from-zero-to-hero-microsoft-extensions-ai](https://github.com/Dometrain/from-zero-to-hero-microsoft-extensions-ai/tree/main/6.%20Generating%20Images?ref=codetraveler.io)

Next week, we'll answer the big question: how do we know if our AI's answers are any *good*? Enter `Microsoft.Extensions.AI.Evaluation`. Make sure to subscribe to this blog so you don't miss it! 👇

To learn even more about Microsoft Extensions AI, check out my course on Dometrain:

[Learn to use Microsoft.Extensions.AI in .NET - DometrainMaster the main AI abstraction library for .NET AI development![](https://storage.ghost.io/c/dc/c2/dcc2e43e-24f8-46bb-a899-9c446f3ef794/content/images/icon/favicon-32x32-ff036adf-4de9-4669-9acf-c8a3731e4f4a.png)DometrainBrandon Minnick![](https://storage.ghost.io/c/dc/c2/dcc2e43e-24f8-46bb-a899-9c446f3ef794/content/images/thumbnail/thumbnail-e14b39ca760b44139b0be9246741f6cb-4f6fb610-8874-49e1-af67-a9149c7f8cca.jpeg)](https://dometrain.com/course/from-zero-to-hero-microsoft-extensions-ai-in-dotnet?ref=codetraveler.io)