Skip to content

Image Generation

Gunpal Jain edited this page Nov 15, 2026 · 3 revisions

Image Generation

Gemini's native image models (the "Nano Banana" family) create and edit images through the normal generateContent call: you send text, optionally with images, and the response contains image parts. This works on both the Gemini API and Vertex AI.

The older Imagen models are retired on both the Gemini API and Vertex AI (Vertex AI retired them on June 30, 2026). Use the Gemini image models described on this page.

NuGet packages

dotnet add package Google_GenerativeAI             # core SDK
dotnet add package Google_GenerativeAI.Microsoft   # optional: IImageGenerator for Microsoft.Extensions.AI

Models

Constant (Gemini API: GoogleAIModels) Model ID Notes
GeminiNanoBanana21 models/gemini-nano-banana-2.1 Image generation and editing (GA); default for GenerativeAIImageGenerator
Gemini3ProImage models/gemini-3-pro-image GA
Gemini31FlashImage models/gemini-3.1-flash-image GA
Gemini31FlashLiteImage models/gemini-3.1-flash-lite-image GA
Gemini25FlashImage models/gemini-2.5-flash-image Scheduled for shutdown on March 15, 2027

On Vertex AI use VertexAIModels.Gemini.GeminiNanoBanana21 or VertexAIModels.Gemini.Gemini3ProImage. See Supported Models for the full list.

Generating an image

Ask for image output with ResponseModalities and read the image from the InlineData parts of the response.

using GenerativeAI;
using GenerativeAI.Types;

var model = new GenerativeModel(apiKey, GoogleAIModels.GeminiNanoBanana21);

var request = new GenerateContentRequest
{
    GenerationConfig = new GenerationConfig
    {
        ResponseModalities = new List<Modality> { Modality.TEXT, Modality.IMAGE },
        ImageConfig = new ImageConfig
        {
            AspectRatio = ImageConfigValues.AspectRatios.Landscape16x9,
            ImageSize = ImageConfigValues.ImageSizes.Size2K
        }
    }
};
request.AddText("A watercolor painting of a lighthouse at sunrise");

var response = await model.GenerateContentAsync(request);

var index = 0;
foreach (var part in response.Candidates?[0].Content?.Parts ?? new List<Part>())
{
    if (part.InlineData?.Data is { } base64)
        await File.WriteAllBytesAsync($"image_{index++}.png", Convert.FromBase64String(base64));
    else if (part.Text is { } text)
        Console.WriteLine(text);
}

Blob.Data is base64 encoded; Blob.MimeType tells you the format (for example image/png).

ImageConfig options

Property Values Notes
AspectRatio "1:1", "16:9", "9:16", "4:3", "3:4", ... Constants in ImageConfigValues.AspectRatios
ImageSize "1K", "2K", "4K" Constants in ImageConfigValues.ImageSizes; only some models support it
ImageOutputOptions MimeType (image/png, image/jpeg), CompressionQuality (0-100, JPEG only) Constants in ImageConfigValues.OutputMimeTypes; only some models support it
PersonGeneration for example "ALLOW_ADULT", "DONT_ALLOW" Vertex AI only
ProminentPeople ProminentPeople.ALLOW_PROMINENT_PEOPLE, BLOCK_PROMINENT_PEOPLE Vertex AI only
OutputMimeType, OutputCompressionQuality MIME type, 0-100 Vertex AI only

The ImageConfigValues constants are for convenience; any string the API accepts can be used.

You can also set the GenerationConfig once on the model (new GenerativeModel(apiKey, GoogleAIModels.GeminiNanoBanana21, config: ...)) and then call model.GenerateContentAsync("prompt").

Editing an image

Send the source image together with the instruction:

var request = new GenerateContentRequest
{
    GenerationConfig = new GenerationConfig
    {
        ResponseModalities = new List<Modality> { Modality.TEXT, Modality.IMAGE }
    }
};
request.AddText("Replace the sky with a starry night, keep everything else the same");
request.AddInlineFile("lighthouse.png"); // appended to the same user turn

var response = await model.GenerateContentAsync(request);

For large or reused images, upload them first and reference them with AddRemoteFile (see Files Client and Multimodal Features). For step-by-step editing across several turns, use a Chat Session created from the same model.

Microsoft.Extensions.AI: GenerativeAIImageGenerator

GenerativeAIImageGenerator (package Google_GenerativeAI.Microsoft, namespace GenerativeAI.Microsoft) implements MEAI's IImageGenerator with the Gemini image models. It requests text and image output and returns the images as DataContent.

#pragma warning disable MEAI001 // IImageGenerator is experimental in Microsoft.Extensions.AI
using GenerativeAI;
using GenerativeAI.Microsoft;
using Microsoft.Extensions.AI;

IImageGenerator generator = new GenerativeAIImageGenerator(apiKey); // defaults to GoogleAIModels.GeminiNanoBanana21

var result = await generator.GenerateAsync(
    new ImageGenerationRequest("A cozy reading nook with a cat, digital art"),
    new ImageGenerationOptions { Count = 1 });

foreach (var image in result.Contents.OfType<DataContent>())
    await File.WriteAllBytesAsync("nook.png", image.Data.ToArray());
  • Constructors: GenerativeAIImageGenerator(string apiKey, string modelName = GoogleAIModels.GeminiNanoBanana21) and GenerativeAIImageGenerator(IPlatformAdapter adapter, string modelName = GoogleAIModels.GeminiNanoBanana21) (use the adapter overload for Vertex AI).
  • Set ImageGenerationRequest.OriginalImages to edit existing images.
  • ImageGenerationOptions.Count sets the candidate count. ImageSize is mapped to a media resolution (low, medium or high). MediaType is not used, because Gemini can't set the image output format through it.
  • For full control, pass a GenerationConfig through ImageGenerationOptions.RawRepresentationFactory; for example to set ImageConfig.
  • The raw GenerateContentResponse is available in ImageGenerationResponse.RawRepresentation, and the underlying GenerativeModel in the model property.

With GenerativeAIChatClient you can also get images from a chat by setting AdditionalPropertiesKeys.ResponseModalities and the ImageConfig* keys in ChatOptions.AdditionalProperties (see Microsoft.Extensions.AI IChatClient).

Imagen (retired)

Google has retired every Imagen model: on the Gemini API, and on Vertex AI on June 30, 2026, where Google names gemini-2.5-flash-image as the replacement. The SDK's Imagen APIs are therefore marked [Obsolete] and produce warning CS0618:

Obsolete API Use instead
ImagenModel, GenAI.CreateImageModel(...), ImageTextModel, VertexAI.CreateImageTextModel() CreateGenerativeModel with a Gemini image model and the IMAGE response modality (see above)
GenerativeAIImagenGenerator (Microsoft.Extensions.AI) GenerativeAIImageGenerator
GoogleAIModels.Imagen.*, VertexAIModels.Imagen.* (removed in 4.0.1) GoogleAIModels.GeminiNanoBanana21, Gemini3ProImage or Gemini31FlashImage, and the matching VertexAIModels.Gemini constants

Requests to Imagen models fail with an ApiException (HTTP 404), so migrate any remaining Imagen code to the Gemini image models.

Legacy: image captioning and visual question answering

ImageTextModel (created with vertexAi.CreateImageTextModel()) calls Vertex AI's legacy imagetext model for captions (GenerateImageCaptionFromLocalFileAsync, GenerateImageCaptionFromGcsImageAsync) and visual Q&A (VisualQuestionAnsweringFromLocalFileAsync, VisualQuestionAnsweringFromGcsFileAsync). For new code, send the image to a Gemini model instead (see Multimodal Features).

Related pages

Clone this wiki locally