Generate DALL-E Images with .NET C# Console Application

This Azure OpenAI article will show you how to generate DALL-E images with .NET C# Console application using the Azure SDK for .NET.

Azure SDK for .NET allows us to build Gen-AI applications using .NET, the C# programming language. We, the SDK, can build Blazor and Console apps using C# and mobile apps.

DALL-E version 3 allows us to generate high-definition images from a text input. Currently, we can access the service from the Azure OpenAI studio using the DALL-E playground and RAST API with Postman and C#.

Azure SDK for .NET

The Azure SDK for .NET allows us to create and manage Azure resources using the .NET language. To create and manage Azure OpenAI service, we use Azure.AI.The OpenAI package library. The libraries are available in NuGet.

Generate DALL-E Images with .NET C# Console Application

To generate DALL-E images, we first need to create an Azure OpenAI resource with an OpenAI deployment. Once your service is deployed, note the endpoint and API Key.

For the application to run, we need to create two environment variables. One is for the Azure OpenAI resource endpoint, and the second is for the API Key.

Below is an example of a PowerShell code that creates the two environment variables.

$env:AZURE_OPENAI_ENDPOINT="Endpoint"
$env:AZURE_OPENAI_API_KEY="API Key"

If you use VS Code, run the code from the VS Code terminal window.

Install-Package

After creating a C# console application, install the Azure OpenAI NuGet library.

dotnet add package Azure.AI.OpenAI --version 1.0.0-beta.17

Program.cs

The following code will generate an Image from the Azure OpenAI DALL-E model. Make sure you set the DeploymentName to the name of your DALL-E deployment name.

In the Prompt, describe the name of the image you would like to generate and run the application.

using static System.Environment;
using System.IO;
using System.Threading.Tasks;
using Azure.AI.OpenAI;

namespace Azure.AI.OpenAI.Tests.Samples
{
    public partial class GenerateImages
    {
        // Get the environment variable
        public static async Task Main(string[] args)
        {
            string endpoint = GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT");
            string key = GetEnvironmentVariable("AZURE_OPENAI_API_KEY");

            OpenAIClient client = new(new Uri(endpoint), new AzureKeyCredential(key));

            // Get the image generations asynchronously
            Response<ImageGenerations> imageGenerations = await client.GetImageGenerationsAsync(
                new ImageGenerationOptions()
                {
                    DeploymentName = "dall-e-3",
                    Prompt = "Createa blog post cover for a blog post about DALL-E",
                    Size = ImageSize.Size1024x1024,

                });

           
            Uri imageUri = imageGenerations.Value.Data[0].Url;
            
           
            Console.WriteLine(imageUri);
        }
    }
}

After running the application, you will get a URL that contains the generated image. The link will be valid for 60 minutes and will be deleted after.

3 thoughts on “Generate DALL-E Images with .NET C# Console Application”

Leave a Comment