openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.0.0-beta.13

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

examples/Images/Example01_SimpleImageGenerationAsync.cs

38lines · modeblame

9f9f2936Jose Arriaga Maldonado2 years ago1using NUnit.Framework;
2using OpenAI.Images;
3using System;
4using System.IO;
5using System.Threading.Tasks;
6
8cc6643fJose Arriaga Maldonado2 years ago7namespace OpenAI.Examples;
74848f8dJose Arriaga Maldonado2 years ago8
8cc6643fJose Arriaga Maldonado2 years ago9public partial class ImageExamples
9f9f2936Jose Arriaga Maldonado2 years ago10{
74848f8dJose Arriaga Maldonado2 years ago11[Test]
8cc6643fJose Arriaga Maldonado2 years ago12public async Task Example01_SimpleImageGenerationAsync()
9f9f2936Jose Arriaga Maldonado2 years ago13{
74848f8dJose Arriaga Maldonado2 years ago14ImageClient client = new("dall-e-3", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
9f9f2936Jose Arriaga Maldonado2 years ago15
74848f8dJose Arriaga Maldonado2 years ago16string prompt = "The concept for a living room that blends Scandinavian simplicity with Japanese minimalism for"
17+ " a serene and cozy atmosphere. It's a space that invites relaxation and mindfulness, with natural light"
18+ " and fresh air. Using neutral tones, including colors like white, beige, gray, and black, that create a"
19+ " sense of harmony. Featuring sleek wood furniture with clean lines and subtle curves to add warmth and"
20+ " elegance. Plants and flowers in ceramic pots adding color and life to a space. They can serve as focal"
21+ " points, creating a connection with nature. Soft textiles and cushions in organic fabrics adding comfort"
22+ " and softness to a space. They can serve as accents, adding contrast and texture.";
9f9f2936Jose Arriaga Maldonado2 years ago23
74848f8dJose Arriaga Maldonado2 years ago24ImageGenerationOptions options = new()
25{
26Quality = GeneratedImageQuality.High,
27Size = GeneratedImageSize.W1792xH1024,
28Style = GeneratedImageStyle.Vivid,
29ResponseFormat = GeneratedImageFormat.Bytes
30};
9f9f2936Jose Arriaga Maldonado2 years ago31
74848f8dJose Arriaga Maldonado2 years ago32GeneratedImage image = await client.GenerateImageAsync(prompt, options);
33BinaryData bytes = image.ImageBytes;
9f9f2936Jose Arriaga Maldonado2 years ago34
74848f8dJose Arriaga Maldonado2 years ago35using FileStream stream = File.OpenWrite($"{Guid.NewGuid()}.png");
36bytes.ToStream().CopyTo(stream);
9f9f2936Jose Arriaga Maldonado2 years ago37}
38}