DALL E

azure-openai-samplesBasic_Samplesdotnetdall-ecsharp

Azure DALLĀ·E image generation example

This notebook shows how to generate images with the Azure OpenAI service.

Installation

Install the Azure Open AI SDK using the below command.

[2]
[ ]

Run this cell, it will prompt you for the apiKey, endPoint, and imageGeneration deployment name

[4]

Import namesapaces and create an instance of OpenAiClient using the azureOpenAIEndpoint and the azureOpenAIKey

[5]
[6]

Import SkiaSharp to display images

[7]

The following code is a function that fetches an image from a given URL and draws it onto a canvas using the SkiaSharp library. SkiaSharp is a cross-platform 2D graphics API for .NET platforms based on Google's Skia Graphics Library. It provides a comprehensive 2D API that can be used across mobile, server, and desktop models to render images.

The function takes three parameters: a string representing the URL of the image, and two int values representing the width and height of the image.

Inside the function, an SKImageInfo object is created with the provided width and height. This object holds information about the image. An SKSurface object is then created using the SKImageInfo object. This surface will be used to draw the image.

An HttpClient object is created to send HTTP requests and receive HTTP responses from the image URL. The GetStreamAsync method is used to send a GET request to the specified Uri and return the response body as a stream in an asynchronous operation.

The image data from the stream is copied into a MemoryStream object. The MemoryStream object is then set back to the beginning using the Seek method. The image data is decoded into an SKBitmap object using the SKBitmap.Decode method.

The bitmap is then drawn onto the canvas at the coordinates (0,0) using the DrawBitmap method. The surface is then drawn onto the canvas at the coordinates (0,0) using the Draw method.

Finally, the method returns the SKSurface object, which now contains the drawn image.

[8]

Generations

With setup and authentication complete, you can now generate images on the Azure OpenAI service and retrieve them from the returned URLs.

1. Generate the images

The first step in this process is to actually generate the images:

[9]

.2 Display

create the image using the response and SkiaSharp and display it

[10]