Use Azure OpenAI to generate images using C#

Sebastian Jensen
medialesson
Published in
5 min readAug 14, 2023

Microsoft enables the options to generate images using Azure OpenAI. In this post I will show you how you can easily use the REST API to generate images using artificial intelligence by writing a simple console application in C# to connect to your Azure OpenAI instance.

Introduction

Microsoft introduced the possibility to generate images using DALL-E within your Azure OpenAI service. In this post I will show you how you can use the REST API to generate images by writing a simple console application.

Request access to Azure OpenAI

Currently you need to request access to the Azure OpenAI Services and have an active Azure Subscription. If this is the case you need to fill out the request form from Microsoft. You need to answer different questions and give Microsoft some time to accept your request.

Add needed Azure resources

If you have access to the Azure OpenAI services, you can open the Azure Portal and start adding an OpenAI resource. Search in the Marketplace for Azure OpenAI. Make sure that you select the subscription which has access to the Azure OpenAI services.

Currently DALL-E is only available in the East US region, so make sure to select East US.

During the next steps you don’t need to change anything. So just leave the default values. On the last page press the Create button.

Open the newly created resource in the Azure Portal and on the Overview page just press the Go to Azure OpenAI Studio link or the Explore button.

In the Azure AI Studio you will find on the left-hand side the menu item DALL-E (Preview). Just click on that.

Here you can easily try out the image generation process within your browser. But we want to create a custom console application, so we click on View code to get access to the api key and the endpoint. From the endpoint we need the resource name, so in my case image-generation-demo and we also need the key. Please copy both values.

Write a console application in C#

Now that we setup all Azure resources we can start Visual Studio and implement a simple console application in C# to be able to generate images.

Create a new Console application. I will name it AzureOpenAIImageConsole, but you can choose your prefered name.

Add the System.CommandLine NuGet package to your solution. This package is currently only available as a preview version, so make sure to select the Include prerelease checkbox.

Add a Models folder. Within this folder we need four classes. Let’s start with the AppConsole.cs:

Next we create the class AddOptions.cs in our Models folder. This class contains all the different options we can pass to our command.

The third class is the AzureOpenAIImageRequestItem.cs this class represents the body of the POST request to start the image generation process.

The last class is called AzureOpenAIImageResponseItem.cs and contains the answer from the REST API.

To increase readability, we create a new file Program.Options.cs in the main directory. We remove the namespace declaration and mark the class as static and partial.

This part of the Program.cs file declares the different arguments that we can use within our command. We also define our RootCommand and provide a method, called GetParsedAppOptions to get the values from the arguments.

We will now create another file called Program.Helpers.cs. This file contains some helper methods. For example we use a method to create an HttpClient with the correct headers, like the api-key header. We also use methods to generate the correct endpoints for the different operations or mapping the size argument the correct value used for the API. We also include methods to extract the needed information from the HttpResponseMessage.

Now we can start with the implementation of the “real” Program.cs file. At first we want to add a little bit of validation to check if all needed arguments are provided.

For each call we need at least the AzureOpenAIResource and the AzureOpenAIKey. If we want to delete images we need to provide the --delete or -d argument. If this is provided we can end the validation, otherwise we check all the different values and make sure that also the range for the size and the amount is set correctly.

In the next step we use the SetHandler method to define the logic of our command.

First we get the parsed options. Next we create the HttpClient and check if the delete option is provided. If this is the case we delete the images using the HttpClient and finish the execution.

If the delete option is not provided, we create the HttpRequestMessage and send this to the API. We get an id as a reponse, which will be printed on the console. Then we need to check regularly if the images are generated. If this is the case we will write the URLs to the provided OutputFilePath.

Now we can try out our command. Just open a Windows Terminal, navigate to the folder containing the csproj file and use dotnet build with our provideded arguments to start the image generation process.

In the provided output you will find links to a blob storage from Microsoft containing your generated image. This is the result for my prompt:

To delete the images just provide the --delete option and the id, which is also written to the output file.

Conclusion

In this blog post we’ve created a simple console application in C# to generate images using an Azure OpenAI service. You will find the source code in this GitHub respository.

If you are interested more into Azure OpenAI don’t hesitate to take a look at my other posts:

--

--

Sebastian Jensen
medialesson

Senior Software Developer & Team Lead @ medialesson GmbH