The integration of artificial intelligence technologies into .NET applications has become increasingly common, driving innovation and creativity across various fields, from graphic design to game development. A major addition to this landscape is the DALL-E API, developed by OpenAI.
In this article, we will explore the integration of the DALL-E API into .NET applications. We will understand what DALL-E is, how it works, and how we can leverage its capabilities to create stunning images directly from our .NET applications.
What is DALL-E?
DALL-E is a tool developed by OpenAI for generating unique images and illustrations from a user’s textual input. In other words, the user provides a text description of what they want to be created; the text is then encoded into a numerical representation, and a generative neural network—trained on a vast dataset of text and images—begins generating a corresponding image. This is achieved through a series of neural network layers that transform the input vector into a visual representation.
The name “DALL-E” is a combination of “Dali” and “WALL-E.”
- “Dali” refers to the famous Spanish surrealist painter Salvador Dalí, known for his distinct and imaginative artwork.
- “WALL-E” refers to the main character of Pixar’s animated film WALL-E, a curious and inventive robot that develops a personality and a sense of creativity throughout the movie.
Therefore, the name “DALL-E” evokes the idea of an artificial intelligence tool capable of generating artistically creative and unique images, paying homage to Salvador Dalí’s imagination and WALL-E’s inventiveness.
Implementation Ideas for Projects
Now that we have seen how DALL-E works and the origin of its name, we can think about ways to implement this image generation tool in our .NET applications.
Below, we will implement a basic code example for a .NET application that makes HTTP requests to the DALL-E API. The response from this request can be processed and implemented, for instance, in real-time image generation websites for children’s stories, where an image is generated based on the story provided.
Another possibility is generating 2D game characters based on appearance and personality descriptions. This would be particularly useful for game developers looking for an efficient way to create a variety of unique and interesting characters.
It is also possible to implement personalized avatar creation for user profiles, offering the option to generate unique and customized avatars based on the information provided during registration. These avatars can be adapted according to user preferences regarding physical characteristics, hairstyle, clothing, and accessories, providing a personalized visual representation.
How to Integrate with DALL-E
The integration with DALL-E is done through its RESTful API. Through this API, we send requests containing textual descriptions and receive generated images in response. It is essential to highlight that these requests must be authorized using an API key, which is available through OpenAI’s website. Thus, when integrating DALL-E into our applications, we can use HTTP clients to interact directly with the API.
Creating the .NET Project
For our example application, we will use an API project built in ASP.NET. Below, we see the basic structure of our application, where we will add our code. In this example, we have a controller called GenerateImageController and a POST action named GenerateImg, which receives a parameter called query—the message we will send to DALL-E. We also add a try-catch block to handle any potential errors in our application.
using Microsoft.AspNetCore.Mvc;
using System.Text;
using System.Text.Json;
namespace GenerateImageDALL_E.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class GenerateImageController : Controller
{
[HttpPost]
public async Task GenerateImg(string query)
{
try
{
//...main code here
}
catch (Exception ex)
{
return StatusCode(500, $"Error generating image: {ex.Message}");
}
}
}
}
With our base structure defined, let’s now implement the logic for the GenerateImg action. The first step is to store our API key, generated on the OpenAI website, in a string so that we can use it in our requests. In real projects, it is recommended to store the key in a secure location.
string openaiApiKey = "your key";
Next, we will start building the body of our request, which will be created using a dynamic object where we will add the required fields.
var requestBody = new
{
model = "dall-e-3",
prompt = query,
n = 1,
size = "1024x1024",
quality = "hd"
};
In the “model” field, we can specify which model we want to use. In this case, we choose “dall-e-3”. An alternative would be the “dall-e-2” model, but it has lower performance.
In the “prompt” field, we will insert the message content, which in this case is the value received in our endpoint through the “query” parameter.
The “n” field specifies how many images will be generated. We can request one image at a time with “dall-e-3” (it’s possible to request more by making parallel requests) or up to 10 images at a time using “dall-e-2”.
In the “size” field, we define the image dimensions. The available sizes are 1024×1024, 1024×1792, or 1792×1024 pixels.
If you don’t specify the “quality” field, the image will be generated as “standard” by default. However, when using DALL·E 3, you can set the quality to “hd”, which creates images with enhanced details.
Now, we need to serialize our “requestBody” object so we can send it in our request.
var requestBodyJson = JsonSerializer.Serialize(requestBody);
After creating the JSON, we will create an HTTP client to send the request to the OpenAI API.
using (var httpClient = new HttpClient())
{
//our code
}
The request code will be divided into three parts. First, we add our authorization key to the request header:
httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {openaiApiKey}");
Next, we send the request using the POST method:
var response = await httpClient.PostAsync("https://api.openai.com/v1/images/generations",
new StringContent(requestBodyJson, Encoding.UTF8, application/json"));
Finally, we validate whether the request was successful. If it was, we read the response as a string and deserialize it into a JsonElement. After that, we extract only the image URL and return a success status along with the image link in our API. If the request fails, we return a BadRequest status.
if (response.IsSuccessStatusCode)
{
string responseBody = await response.Content.ReadAsStringAsync();
var responseObject = JsonSerializer.Deserialize(responseBody);
string content = responseObject.GetProperty("data")[0].GetProperty("url").GetString();
return Ok(content);
}
else
{
return BadRequest(response.StatusCode);
}
When running the application, we can send a query and receive a link to an image. The image URLs expire after 1 hour, so depending on the application, it is recommended to save the image.
Below is an example of a request in Swagger:
And by opening the image URL, we get the following result:

In this article, we saw a demonstration of the power of artificial intelligence when combined with the robustness and flexibility of the .NET environment, serving as a starting point for developing a wide range of scenarios, from creating artwork for simple websites to generating visual content for large-scale software applications. By leveraging the capabilities of both areas, developers can create smarter, more efficient, and adaptable applications, thus driving the next generation of technology.