Building Image Generation Apps with Azure OpenAI in .NET

In this article, we will explore how to use Azure OpenAI DALL-E service in .NET to generate images based on user prompts. By leveraging Azure OpenAI’s powerful DALL·E model, we can create unique image content dynamically based on our prompt. We'll walk through an example of how to connect to Azure’s OpenAI API, submit a prompt, and retrieve generated images in .NET Applications.

Prerequisites

Before we start, ensure we have the following.

  • Azure Subscription: You will need access to the Azure OpenAI service.
  • OpenAI Resource: Set up an OpenAI resource in your Azure account.
  • API Key and Endpoint: Retrieve the key and endpoint for your Azure OpenAI resource.
  • Visual Studio or .NET SDK: Make sure you have a working .NET environment.

Step 1. Set up Azure OpenAI in Azure Portal

  1. Create an OpenAI resource in Azure
    • Navigate to the Azure Portal, search for "Azure OpenAI" and click Create.
    • Provide basic details like resource group, region, and pricing tier.
    • After the resource is deployed, you can find the API endpoint and key in the Keys and Endpoints section.
      Endpoints section
  2. Deploy DALL-E: Under our OpenAI resource, go to the Models deployments section and deploy the `dall-e-3` model. We will use this in the API calls later in our applications.
    OpenAI

Step 2. Create a .NET C# Project

  1. Create a new Console Application: Open Visual Studio and create a new Console App using .NET 6.0+.
     Visual Studio
  2. Install NuGet Packages
    • Install the client library for .NET with NuGet:
    • dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.12

Step 3. Write Code to DALL-E App

using Azure.AI.OpenAI;
using Azure;
namespace GenerativeAI
{
    public class AzureOpenAIGPT
    {
        // Replace with your own key and endpoint
        const string key = "***********************************";
        const string endpoint = "https://*************.openai.azure.com/";
        const string deploymentOrModelName = "imagedemo";
        public async Task<string> GetContent(string prompt)
        {
            var client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
            var imageGenerationOptions = new ImageGenerationOptions
            {
                Prompt = prompt,
                DeploymentName = deploymentOrModelName,
                Size = new ImageSize("1024x1024"),
                ImageCount = 1
            };
            var response = await client.GetImageGenerationsAsync(imageGenerationOptions);
            return response.Value.Data[0].Url.AbsoluteUri;
        }
    }
    class Program
    {
        static async Task Main(string[] args)
        {
            var dalle = new AzureOpenAIGPT();
            var dallePrompt = "Cat eating cake on the sky";
            Console.WriteLine($"{await dalle.GetContent(dallePrompt)}");
            Console.ReadLine();
        }
    }
}

Key Components

  1. API Key and Endpoint: The key and endpoint are critical to authenticate and communicate with Azure OpenAI. These values are specific to our Azure subscription and OpenAI resource.
    const string key = "your-azure-key";
    const string endpoint = "your-endpoint";
  2. OpenAIClient Initialization: The OpenAIClient is the main client for interacting with Azure OpenAI services. It requires the endpoint and the Azure API key.
    var client = new OpenAIClient(new Uri(endpoint), new AzureKeyCredential(key));
  3. Image Generation Options: This section configures the prompt and other parameters for image generation. In this example, we use the "image demo" model to generate a 1024x1024 image based on the user prompt.
    var imageGenerationOptions = new ImageGenerationOptions
    {
        Prompt = prompt,
        DeploymentName = deploymentOrModelName,
        Size = new ImageSize("1024x1024"),
        ImageCount = 1
    };
  4. Retrieving and Displaying the Image URL: The generated image is returned as a URL, which can be directly opened or embedded into a web page.
    ​​​​​​​var response = await client.GetImageGenerationsAsync(imageGenerationOptions);
    return response.Value.Data[0].Url.AbsoluteUri;
  5. Console Output: The Main function runs the code and outputs the image URL to the console, allowing us to view the generated content.
    ​​​​​​​Console.WriteLine($"{await dalle.GetContent(dallePrompt)}");

Namespace

Output

Conclusion

This simple application for how to use Azure OpenAI’s DALL·E model to generate images in a .NET environment. By integrating Azure OpenAI into our .NET projects, we can unlock powerful generative AI capabilities that can enhance user experiences, automate content creation, and build innovative applications.