-
Notifications
You must be signed in to change notification settings - Fork 6k
Add tutorial for text-to-image generation using Microsoft.Extensions.AI #49391
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Copilot
wants to merge
8
commits into
main
Choose a base branch
from
copilot/add-ai-text-to-image-tutorial
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
065df1c
Initial plan
Copilot 62f12d5
Add text-to-image tutorial with code examples
Copilot 30aed3c
Update tutorial documentation with correct property descriptions
Copilot 4abcec8
remove openai example and make it a quickstart
gewarren f9f2395
fix snippet tags
gewarren c341a8e
add more next steps
gewarren bbc3711
respond to feedback; update links
gewarren c9b4f4e
update project file
gewarren File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions
46
docs/ai/quickstarts/snippets/text-to-image/azure-openai/Program.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| // <SnippetConfigClient> | ||
| using Azure; | ||
| using Azure.AI.OpenAI; | ||
| using Microsoft.Extensions.AI; | ||
| using Microsoft.Extensions.Configuration; | ||
|
|
||
| IConfigurationRoot config = new ConfigurationBuilder() | ||
| .AddUserSecrets<Program>() | ||
| .Build(); | ||
|
|
||
| string endpoint = config["AZURE_OPENAI_ENDPOINT"]; | ||
| string apiKey = config["AZURE_OPENAI_API_KEY"]; | ||
| string model = config["AZURE_OPENAI_GPT_NAME"]; | ||
|
|
||
| // Create the Azure OpenAI client and convert to IImageGenerator. | ||
| AzureOpenAIClient azureClient = new( | ||
| new Uri(endpoint), | ||
| new AzureKeyCredential(apiKey)); | ||
|
|
||
| var imageClient = azureClient.GetImageClient(model); | ||
| #pragma warning disable MEAI001 // Type is for evaluation purposes only. | ||
| IImageGenerator generator = imageClient.AsIImageGenerator(); | ||
| // </SnippetConfigClient> | ||
|
|
||
| // <SnippetGenerateImage> | ||
| // Generate an image from a text prompt | ||
| var options = new ImageGenerationOptions | ||
| { | ||
| MediaType = "image/png" | ||
| }; | ||
| string prompt = "A tennis court in a jungle"; | ||
| var response = await generator.GenerateImagesAsync(prompt, options); | ||
|
|
||
| // Save the image to a file. | ||
| var dataContent = response.Contents.OfType<DataContent>().First(); | ||
| string fileName = SaveImage(dataContent, "jungle-tennis.png"); | ||
| Console.WriteLine($"Image saved to file: {fileName}"); | ||
|
|
||
| static string SaveImage(DataContent content, string fileName) | ||
| { | ||
| string userDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); | ||
| var path = Path.Combine(userDirectory, fileName); | ||
| File.WriteAllBytes(path, content.Data.ToArray()); | ||
| return Path.GetFullPath(path); | ||
| } | ||
| // </SnippetGenerateImage> | ||
17 changes: 17 additions & 0 deletions
17
docs/ai/quickstarts/snippets/text-to-image/azure-openai/TextToImageAzureOpenAI.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <OutputType>Exe</OutputType> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" /> | ||
| <PackageReference Include="Microsoft.Extensions.AI.OpenAI" Version="9.10.1-preview.1.25521.4" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.10" /> | ||
| <PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="9.0.10" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| --- | ||
| title: Quickstart - Generate images from text using AI | ||
| description: Learn how to use Microsoft.Extensions.AI to generate images from text prompts using AI models in a .NET application. | ||
| ms.date: 10/21/2025 | ||
| ms.topic: quickstart | ||
| ai-usage: ai-assisted | ||
| --- | ||
|
|
||
| # Generate images from text using AI | ||
|
|
||
| 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. | ||
|
|
||
| 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: | ||
|
|
||
| - Text-to-image generation. | ||
| - Pipeline composition with middleware (logging, telemetry, caching). | ||
| - Flexible configuration options. | ||
| - Support for multiple AI providers. | ||
|
|
||
| > [!NOTE] | ||
| > 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. | ||
|
|
||
| <!--Prereqs--> | ||
| [!INCLUDE [azure-openai-prereqs](../quickstarts/includes/prerequisites-azure-openai.md)] | ||
|
|
||
| ## Configure the AI service | ||
|
|
||
| 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. | ||
gewarren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| > [!NOTE] | ||
| > `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). | ||
|
|
||
| ## Create the application | ||
|
|
||
| Complete the following steps to create a .NET console application that generates images from text prompts. | ||
|
|
||
| 1. Create a new console application: | ||
|
|
||
| ```dotnetcli | ||
| dotnet new console -o TextToImageAI | ||
| ``` | ||
|
|
||
| 1. Navigate to the `TextToImageAI` directory, and add the necessary packages to your app: | ||
|
|
||
| ```dotnetcli | ||
| dotnet add package Azure.AI.OpenAI | ||
| dotnet add package Microsoft.Extensions.AI.OpenAI --prerelease | ||
| dotnet add package Microsoft.Extensions.Configuration | ||
| dotnet add package Microsoft.Extensions.Configuration.UserSecrets | ||
| ``` | ||
|
|
||
| 1. Run the following commands to add [app secrets](/aspnet/core/security/app-secrets) for your Azure OpenAI endpoint, model name, and API key: | ||
|
|
||
| ```bash | ||
| dotnet user-secrets init | ||
| dotnet user-secrets set AZURE_OPENAI_ENDPOINT <your-Azure-OpenAI-endpoint> | ||
| dotnet user-secrets set AZURE_OPENAI_GPT_NAME gpt-image-1 | ||
gewarren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| dotnet user-secrets set AZURE_OPENAI_API_KEY <your-azure-openai-api-key> | ||
| ``` | ||
|
|
||
| 1. Open the new app in your editor of choice (for example, Visual Studio). | ||
|
|
||
| ## Implement basic image generation | ||
|
|
||
| 1. Update the `Program.cs` file with the following code to get the configuration data and create the <xref:Azure.AI.OpenAI.AzureOpenAIClient>: | ||
|
|
||
| :::code language="csharp" source="snippets/text-to-image/azure-openai/Program.cs" id="ConfigClient"::: | ||
|
|
||
| The preceding code: | ||
|
|
||
| - Loads configuration from user secrets. | ||
| - Creates an `ImageClient` from the OpenAI SDK. | ||
| - Converts the `ImageClient` to an `IImageGenerator` using the <xref:Microsoft.Extensions.AI.OpenAIClientExtensions.AsIImageGenerator(OpenAI.Images.ImageClient)> extension method. | ||
|
|
||
| 1. Add the following code to implement basic text-to-image generation: | ||
|
|
||
| :::code language="csharp" source="snippets/text-to-image/azure-openai/Program.cs" id="GenerateImage"::: | ||
|
|
||
| The preceding code: | ||
|
|
||
| - Sets the requested image file type by setting <xref:Microsoft.Extensions.AI.ImageGenerationOptions.MediaType?displayProperty=nameWithType>. | ||
| - 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. | ||
| - Saves the generated image to a file in the local user directory. | ||
|
|
||
| 1. Run the application, either through the IDE or using `dotnet run`. | ||
|
|
||
| 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. | ||
|
|
||
| :::image type="content" source="media/text-to-image/jungle-tennis.png" alt-text="AI-generated image of a tennis court in a jungle."::: | ||
|
|
||
| ## Configure image generation options | ||
|
|
||
| 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: | ||
|
|
||
| - <xref:Microsoft.Extensions.AI.ImageGenerationOptions.AdditionalProperties>: Provider-specific options. | ||
| - <xref:Microsoft.Extensions.AI.ImageGenerationOptions.Count>: The number of images to generate. | ||
| - <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). | ||
| - <xref:Microsoft.Extensions.AI.ImageGenerationOptions.MediaType>: The media type (MIME type) of the generated image. | ||
| - <xref:Microsoft.Extensions.AI.ImageGenerationOptions.ModelId>: The model ID. | ||
| - <xref:Microsoft.Extensions.AI.ImageGenerationOptions.RawRepresentationFactory>: The callback that creates the raw representation of the image generation options from an underlying implementation. | ||
| - <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>. | ||
|
|
||
| ## Best practices | ||
|
|
||
| When implementing text-to-image generation in your applications, consider these best practices: | ||
|
|
||
| - **Prompt engineering**: Write clear, detailed prompts that describe the desired image. Include specific details about style, composition, colors, and elements. | ||
| - **Cost management**: Image generation can be expensive. Cache results when possible and implement rate limiting to control costs. | ||
| - **Content safety**: Always review generated images for appropriate content, especially in production applications. Consider implementing content filtering and moderation. | ||
| - **User experience**: Image generation can take several seconds. Provide progress indicators and handle timeouts gracefully. | ||
| - **Legal considerations**: Be aware of licensing and usage rights for generated images. Review the terms of service for your AI provider. | ||
|
|
||
| ## Clean up resources | ||
|
|
||
| When you no longer need the Azure OpenAI resource, delete it to avoid incurring charges: | ||
|
|
||
| 1. In the [Azure Portal](https://portal.azure.com), navigate to your Azure OpenAI resource. | ||
| 1. Select the resource and then select **Delete**. | ||
|
|
||
| ## Next steps | ||
|
|
||
| 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: | ||
|
|
||
| - Refining the generated image iteratively. | ||
| - Editing an existing image. | ||
| - Personalizing an image, diagram, or theme. | ||
|
|
||
gewarren marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## See also | ||
|
|
||
| - [Explore text-to-image capabilities in .NET (blog post)](https://devblogs.microsoft.com/dotnet/explore-text-to-image-dotnet/) | ||
| - [Microsoft.Extensions.AI library overview](../microsoft-extensions-ai.md) | ||
| - [Quickstart: Build an AI chat app with .NET](../quickstarts/build-chat-app.md) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.