|
| 1 | +--- |
| 2 | +title: Quickstart - Generate images from text using AI |
| 3 | +description: Learn how to use Microsoft.Extensions.AI to generate images from text prompts using AI models in a .NET application. |
| 4 | +ms.date: 10/21/2025 |
| 5 | +ms.topic: quickstart |
| 6 | +ai-usage: ai-assisted |
| 7 | +--- |
| 8 | + |
| 9 | +# Generate images from text using AI |
| 10 | + |
| 11 | +In this quickstart, you use the <xref:Microsoft.Extensions.AI> (MEAI) library to generate images from text prompts using an AI model. The MEAI text-to-image capabilities let you generate images from natural language prompts or existing images using a consistent and extensible API surface. |
| 12 | + |
| 13 | +The <xref:Microsoft.Extensions.AI.IImageGenerator> interface provides a unified, extensible API for working with various image generation services, making it easy to integrate text-to-image capabilities into your .NET apps. The interface supports: |
| 14 | + |
| 15 | +- Text-to-image generation. |
| 16 | +- Pipeline composition with middleware (logging, telemetry, caching). |
| 17 | +- Flexible configuration options. |
| 18 | +- Support for multiple AI providers. |
| 19 | + |
| 20 | +> [!NOTE] |
| 21 | +> The `IImageGenerator` interface is currently marked as experimental with the `MEAI001` diagnostic ID. You might need to suppress this warning in your project file or code. |
| 22 | +
|
| 23 | +<!--Prereqs--> |
| 24 | +[!INCLUDE [azure-openai-prereqs](../quickstarts/includes/prerequisites-azure-openai.md)] |
| 25 | + |
| 26 | +## Configure the AI service |
| 27 | + |
| 28 | +To provision an Azure OpenAI service and model using the Azure portal, complete the steps in the [Create and deploy an Azure OpenAI Service resource](/azure/ai-services/openai/how-to/create-resource?pivots=web-portal) article. In the "Deploy a model" step, select the `gpt-image-1` model. |
| 29 | + |
| 30 | +> [!NOTE] |
| 31 | +> `gpt-image-1` is a newer model that offers several improvements over DALL-E 3. It's available from OpenAI on a limited basis; apply for access with [this form](https://aka.ms/oai/gptimage1access). |
| 32 | +
|
| 33 | +## Create the application |
| 34 | + |
| 35 | +Complete the following steps to create a .NET console application that generates images from text prompts. |
| 36 | + |
| 37 | +1. Create a new console application: |
| 38 | + |
| 39 | + ```dotnetcli |
| 40 | + dotnet new console -o TextToImageAI |
| 41 | + ``` |
| 42 | +
|
| 43 | +1. Navigate to the `TextToImageAI` directory, and add the necessary packages to your app: |
| 44 | +
|
| 45 | + ```dotnetcli |
| 46 | + dotnet add package Azure.AI.OpenAI |
| 47 | + dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease |
| 48 | + dotnet add package Microsoft.Extensions.Configuration |
| 49 | + dotnet add package Microsoft.Extensions.Configuration.UserSecrets |
| 50 | + ``` |
| 51 | +
|
| 52 | +1. Run the following commands to add [app secrets](/aspnet/core/security/app-secrets) for your Azure OpenAI endpoint, model name, and API key: |
| 53 | +
|
| 54 | + ```bash |
| 55 | + dotnet user-secrets init |
| 56 | + dotnet user-secrets set AZURE_OPENAI_ENDPOINT <your-Azure-OpenAI-endpoint> |
| 57 | + dotnet user-secrets set AZURE_OPENAI_GPT_NAME gpt-image-1 |
| 58 | + dotnet user-secrets set AZURE_OPENAI_API_KEY <your-azure-openai-api-key> |
| 59 | + ``` |
| 60 | +
|
| 61 | +1. Open the new app in your editor of choice (for example, Visual Studio). |
| 62 | +
|
| 63 | +## Implement basic image generation |
| 64 | +
|
| 65 | +1. Update the `Program.cs` file with the following code to get the configuration data and create the <xref:Azure.AI.OpenAI.AzureOpenAIClient>: |
| 66 | +
|
| 67 | + :::code language="csharp" source="snippets/text-to-image/azure-openai/Program.cs" id="ConfigClient"::: |
| 68 | +
|
| 69 | + The preceding code: |
| 70 | +
|
| 71 | + - Loads configuration from user secrets. |
| 72 | + - Creates an `ImageClient` from the OpenAI SDK. |
| 73 | + - Converts the `ImageClient` to an `IImageGenerator` using the <xref:Microsoft.Extensions.AI.OpenAIClientExtensions.AsIImageGenerator(OpenAI.Images.ImageClient)> extension method. |
| 74 | +
|
| 75 | +1. Add the following code to implement basic text-to-image generation: |
| 76 | +
|
| 77 | + :::code language="csharp" source="snippets/text-to-image/azure-openai/Program.cs" id="GenerateImage"::: |
| 78 | +
|
| 79 | + The preceding code: |
| 80 | +
|
| 81 | + - Sets the requested image file type by setting <xref:Microsoft.Extensions.AI.ImageGenerationOptions.MediaType?displayProperty=nameWithType>. |
| 82 | + - Generates an image using the <xref:Microsoft.Extensions.AI.ImageGeneratorExtensions.GenerateImagesAsync(Microsoft.Extensions.AI.IImageGenerator,System.String,Microsoft.Extensions.AI.ImageGenerationOptions,System.Threading.CancellationToken)> method with a text prompt. |
| 83 | + - Saves the generated image to a file in the local user directory. |
| 84 | +
|
| 85 | +1. Run the application, either through the IDE or using `dotnet run`. |
| 86 | +
|
| 87 | + The application generates an image and outputs the file path to the image. Open the file to view the generated image. The following image shows one example of a generated image. |
| 88 | +
|
| 89 | + :::image type="content" source="media/text-to-image/jungle-tennis.png" alt-text="AI-generated image of a tennis court in a jungle."::: |
| 90 | +
|
| 91 | +## Configure image generation options |
| 92 | +
|
| 93 | +You can customize image generation by providing other options such as size, response format, and number of images to generate. The <xref:Microsoft.Extensions.AI.ImageGenerationOptions> class allows you to specify: |
| 94 | +
|
| 95 | +- <xref:Microsoft.Extensions.AI.ImageGenerationOptions.AdditionalProperties>: Provider-specific options. |
| 96 | +- <xref:Microsoft.Extensions.AI.ImageGenerationOptions.Count>: The number of images to generate. |
| 97 | +- <xref:Microsoft.Extensions.AI.ImageGenerationOptions.ImageSize>: The dimensions of the generated image as a <xref:System.Drawing.Size?displayProperty=fullName>. For supported sizes, see the [OpenAI API reference](https://platform.openai.com/docs/api-reference/images/create). |
| 98 | +- <xref:Microsoft.Extensions.AI.ImageGenerationOptions.MediaType>: The media type (MIME type) of the generated image. |
| 99 | +- <xref:Microsoft.Extensions.AI.ImageGenerationOptions.ModelId>: The model ID. |
| 100 | +- <xref:Microsoft.Extensions.AI.ImageGenerationOptions.RawRepresentationFactory>: The callback that creates the raw representation of the image generation options from an underlying implementation. |
| 101 | +- <xref:Microsoft.Extensions.AI.ImageGenerationOptions.ResponseFormat>: Options are <xref:Microsoft.Extensions.AI.ImageGenerationResponseFormat.Uri>, <xref:Microsoft.Extensions.AI.ImageGenerationResponseFormat.Data>, and <xref:Microsoft.Extensions.AI.ImageGenerationResponseFormat.Hosted>. |
| 102 | +
|
| 103 | +## Best practices |
| 104 | +
|
| 105 | +When implementing text-to-image generation in your applications, consider these best practices: |
| 106 | +
|
| 107 | +- **Prompt engineering**: Write clear, detailed prompts that describe the desired image. Include specific details about style, composition, colors, and elements. |
| 108 | +- **Cost management**: Image generation can be expensive. Cache results when possible and implement rate limiting to control costs. |
| 109 | +- **Content safety**: Always review generated images for appropriate content, especially in production applications. Consider implementing content filtering and moderation. |
| 110 | +- **User experience**: Image generation can take several seconds. Provide progress indicators and handle timeouts gracefully. |
| 111 | +- **Legal considerations**: Be aware of licensing and usage rights for generated images. Review the terms of service for your AI provider. |
| 112 | +
|
| 113 | +## Clean up resources |
| 114 | +
|
| 115 | +When you no longer need the Azure OpenAI resource, delete it to avoid incurring charges: |
| 116 | +
|
| 117 | +1. In the [Azure Portal](https://portal.azure.com), navigate to your Azure OpenAI resource. |
| 118 | +1. Select the resource and then select **Delete**. |
| 119 | +
|
| 120 | +## Next steps |
| 121 | +
|
| 122 | +You've successfully generated some different images using the <xref:Microsoft.Extensions.AI.IImageGenerator> interface in <xref:Microsoft.Extensions.AI>. Next, you can explore some of the additional functionality, including: |
| 123 | +
|
| 124 | +- Refining the generated image iteratively. |
| 125 | +- Editing an existing image. |
| 126 | +- Personalizing an image, diagram, or theme. |
| 127 | +
|
| 128 | +## See also |
| 129 | +
|
| 130 | +- [Explore text-to-image capabilities in .NET (blog post)](https://devblogs.microsoft.com/dotnet/explore-text-to-image-dotnet/) |
| 131 | +- [Microsoft.Extensions.AI library overview](../microsoft-extensions-ai.md) |
| 132 | +- [Quickstart: Build an AI chat app with .NET](../quickstarts/build-chat-app.md) |
0 commit comments