Click here to Skip to main content
15,887,135 members
Articles / Artificial Intelligence
Tip/Trick

Harness AI to Effortlessly Generate Custom Images from Text Descriptions in .NET Applications

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
15 Oct 2023CPOL1 min read 5.2K   3   1
Discover how to leverage AI capabilities in .NET applications for generating custom images from textual descriptions with ease.
Explore a novel feature in a handy NuGet package that empowers .NET developers to seamlessly create custom images from textual descriptions using OpenAI's API. Whether you need unique images for your project or wish to reduce reliance on graphic designers, this article guides you through the process. With a few lines of code, transform textual prompts into visual creativity, enhancing your application's interactive and aesthetic appeal. The article also hints at a previous discussion on generating image variations, broadening the spectrum of image manipulation capabilities within .NET applications.

Introduction

This article unveils a new feature in the ConnectingApps.Refit.OpenAI NuGet package that enables developers to create images using OpenAI's API in .NET applications based on textual descriptions. This feature is a boon for those needing to generate custom images without the reliance on graphic designers or stock images.

Background

The ability to generate images from textual descriptions is a remarkable advancement facilitated by OpenAI. The ConnectingApps.Refit.OpenAI NuGet package brings this capability to the fingertips of .NET developers. For a more comprehensive understanding, you may refer to the introductory article on CodeProject. Moreover, if you're interested in generating image variations, the follow-up article on image variations is a worthy read.

Using the Code

Let’s delve into how you can leverage this feature to generate images from textual descriptions in a .NET application.

Firstly, ensure that the ConnectingApps.Refit.OpenAI NuGet package is installed in your project. Now, set up your OpenAI API key as an environment variable:

C#
using System;
using System.Net.Http;
using ConnectingApps.Refit.OpenAI;
using ConnectingApps.Refit.OpenAI.ImageCreations;
using ConnectingApps.Refit.OpenAI.ImageCreations.Request;
using Refit;

// ...

var apiKey = Environment.GetEnvironmentVariable("OPENAI_KEY");

Create an instance of the IImageCreation interface, specifying the base address of the OpenAI API:

C#
var creationApi = RestService.For<IImageCreation>(new HttpClient
{
    BaseAddress = new Uri("https://api.openai.com")
}, OpenAiRefitSettings.RefitSettings);

Now, initiate a call to the CreateImageAsync method, passing in an ImageCreationRequest object with the desired parameters:

C#
var response = await creationApi.CreateImageAsync(new ImageCreationRequest
    {
        N = 2,
        Prompt = "A cute baby sea otter.",
        Size = "1024x1024",
    }, $"Bearer {apiKey}");

This method returns a response containing the status code and URLs of the generated images. You can access and use the URLs as needed:

C#
Console.WriteLine($"Returned response status code {response.StatusCode}");
Console.WriteLine($"Number of urls {response.Content!.Data.Length}" );
Console.WriteLine($"First url {response.Content!.Data.First().Url}");
Console.WriteLine($"Last url {response.Content!.Data.Last().Url}");

The console output will be as follows:

Returned response status code OK
Number of urls 2
First url [FIRST URL]
Last url [SECOND URL]

For more insights and examples, feel free to explore the package’s GitHub repository.

Points of Interest

The seamless interaction between the ConnectingApps.Refit.OpenAI NuGet package and OpenAI’s API simplifies the image generation process, making it an efficient solution for developers needing custom images for their projects.

History

  • 15th October, 2023: Initial version of the article showcasing the new image creation feature of the ConnectingApps.Refit.OpenAI NuGet package

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Netherlands Netherlands
I am a self-employed software engineer working on .NET Core. I love TDD.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Ștefan-Mihai MOGA15-Oct-23 20:09
professionalȘtefan-Mihai MOGA15-Oct-23 20:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.