openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Images/Example01_SimpleImageGenerationAsync.cs
38lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Images; |
| 3 | using System; |
| 4 | using System.IO; |
| 5 | using System.Threading.Tasks; |
| 6 | |
| 7 | namespace OpenAI.Examples; |
| 8 | |
| 9 | public partial class ImageExamples |
| 10 | { |
| 11 | [Test] |
| 12 | public async Task Example01_SimpleImageGenerationAsync() |
| 13 | { |
| 14 | ImageClient client = new("dall-e-3", Environment.GetEnvironmentVariable("OPENAI_API_KEY")); |
| 15 | |
| 16 | string 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."; |
| 23 | |
| 24 | ImageGenerationOptions options = new() |
| 25 | { |
| 26 | Quality = GeneratedImageQuality.High, |
| 27 | Size = GeneratedImageSize.W1792xH1024, |
| 28 | Style = GeneratedImageStyle.Vivid, |
| 29 | ResponseFormat = GeneratedImageFormat.Bytes |
| 30 | }; |
| 31 | |
| 32 | GeneratedImage image = await client.GenerateImageAsync(prompt, options); |
| 33 | BinaryData bytes = image.ImageBytes; |
| 34 | |
| 35 | using FileStream stream = File.OpenWrite($"{Guid.NewGuid()}.png"); |
| 36 | bytes.ToStream().CopyTo(stream); |
| 37 | } |
| 38 | } |
| 39 | |