Add DALL-E to your WinUI 3 / Windows App SDK desktop app

In this how-to, we'll integrate DALL-E's image generation capabilities into your WinUI 3 / Windows App SDK desktop app.

Prerequisites

Install and initialize the OpenAI SDK

Ensure that the betalgo/OpenAI SDK is installed in your project by running dotnet add package Betalgo.OpenAI from Visual Studio's terminal window. Initialize the SDK with your OpenAI API key as follows:

//...
using OpenAI;
using OpenAI.Managers;
using OpenAI.ObjectModels.RequestModels;
using OpenAI.ObjectModels;

namespace ChatGPT_WinUI3
{
    public sealed partial class MainWindow : Window
    {
        private OpenAIService openAiService;

        public MainWindow()
        {
            this.InitializeComponent();
           
            var openAiKey = Environment.GetEnvironmentVariable("OPENAI_API_KEY");

            openAiService = new OpenAIService(new OpenAiOptions(){
                ApiKey = openAiKey
            });
        }
    }
}

Modify your app's UI

Modify your existing MainWindow.xaml to include an Image control that displays images within the conversation:

<!-- ... existing XAML ... -->
<ItemsControl.ItemTemplate>
    <DataTemplate>
        <Image Source="{Binding ImageUrl}" Margin="5" Stretch="UniformToFill"/>
    </DataTemplate>
</ItemsControl.ItemTemplate>
<!-- ... existing XAML ... -->

Note that this how-to assumes you have a chat interface with a TextBox and Button; see How to add OpenAI chat completions to your WinUI 3 / Windows App SDK desktop app.

Implement DALL-E image generation

In your MainWindow.xaml.cs, add the following methods to handle image generation and display:

// ... existing using statements ...

private async void SendButton_Click(object sender, RoutedEventArgs e)
{
    ResponseProgressBar.Visibility = Visibility.Visible;
    string userInput = InputTextBox.Text;
    if (!string.IsNullOrEmpty(userInput))
    {
        InputTextBox.Text = string.Empty;
        var imageResult = await openAiService.Image.CreateImage(new ImageCreateRequest
        {
            Prompt = userInput,
            N = 2,
            Size = StaticValues.ImageStatics.Size.Size256, // StaticValues is available as part of the Betalgo OpenAI SDK
            ResponseFormat = StaticValues.ImageStatics.ResponseFormat.Url,
            User = "TestUser"
        });

        if (imageResult.Successful)
        {
            foreach (var imageUrl in imageResult.Results.Select(r => r.Url))
            {
                AddImageMessageToConversation(imageUrl);
            }
        }
        else
        {
            AddMessageToConversation("GPT: Sorry, something bad happened: " + imageResult.Error?.Message);
        }
    }
    ResponseProgressBar.Visibility = Visibility.Collapsed;
}

private void AddImageMessageToConversation(string imageUrl)
{
    var imageMessage = new MessageItem
    {
        ImageUrl = imageUrl
    };
    ConversationList.Items.Add(imageMessage);
}

The openAiService.Image.CreateImage() method is responsible for calling OpenAI's DALL-E API. Refer to the Betalgo OpenAI SDK wiki for more usage examples.

Note the presence of ImageUrl in the MessageItem class. This is a new property:

public class MessageItem
{
    public string Text { get; set; }
    public SolidColorBrush Color { get; set; }
    public string ImageUrl { get; set; } // new
}

Run and test

Run your app, enter a prompt, and click the "Generate Image" button. You should see something like this:

Image generation demo

Recap

In this guide, you've learned how to:

  1. Accept image prompts from users within a <TextBox>.
  2. Generate images using the OpenAI DALL-E API.
  3. Display the image in an <Image>.