DEV Community

Daniel Gomez
Daniel Gomez

Posted on

5 1 1

Using Azure OpenAI Service to generate images with DALL-E in .NET

Hey, there! In this tutorial, we'll see how to call an Azure OpenAI Service DALL-E model to generate new images from C#.

Required data from Azure:

Key and endpoint - We can find this information in the Keys and endpoint section in our Azure OpenAI Service resource.

Azure Resource - Key and endpoint

Deployment name - This would be the instance of the model to use. In this case, we can go to the Azure OpenAI Service studio, and in the Deployments section specify a new one:

Azure OpenAI Service - Deployments

Azure OpenAI client library for .NET

Azure OpenAI client library for .NET is an adaptation of OpenAI's REST APIs that provides an idiomatic interface and rich integration with the rest of the Azure SDK ecosystem.

This library will allow us to use the GPT model from C#.

NuGet package: Azure.AI.OpenAI

Code in C#

With the NuGet package installed, this is a C# class that will allow us to generate new image using a specified prompt:



using Azure;
using Azure.AI.OpenAI;

namespace GenerativeAI;

public class AzureOpenAIDALLE
{
    const string key = "YOUR_KEY";
    const string endpoint = "https://YOUR_RESOURCE_NAME.openai.azure.com/";
    const string deploymentOrModelName = "YOUR_DEPLOYMENT";

    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;
    }
}


Enter fullscreen mode Exit fullscreen mode

Example:



var dalle = new AzureOpenAIDALLE();

var dallePrompt = "Generate a featured image about Thanksgiving dishes";
Console.WriteLine($"{await dalle.GetContent(dallePrompt)}");


Enter fullscreen mode Exit fullscreen mode

Image generated result

Thanks for reading!

If you have any questions or ideas in mind, it'll be a pleasure to be able to be in communication with you, and together exchange knowledge with each other.

Additional Resources:

Contact:
X / LinkedIn - esdanielgomez.com

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay