openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.1.0-beta.1

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/Images/Example01_SimpleImageGeneration.cs

37lines · modeblame

9f9f2936Jose Arriaga Maldonado2 years ago1using NUnit.Framework;
2using OpenAI.Images;
3using System;
4using System.IO;
5
8cc6643fJose Arriaga Maldonado2 years ago6namespace OpenAI.Examples;
74848f8dJose Arriaga Maldonado2 years ago7
8cc6643fJose Arriaga Maldonado2 years ago8public partial class ImageExamples
9f9f2936Jose Arriaga Maldonado2 years ago9{
74848f8dJose Arriaga Maldonado2 years ago10[Test]
8cc6643fJose Arriaga Maldonado2 years ago11public void Example01_SimpleImageGeneration()
9f9f2936Jose Arriaga Maldonado2 years ago12{
74848f8dJose Arriaga Maldonado2 years ago13ImageClient client = new("dall-e-3", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
9f9f2936Jose Arriaga Maldonado2 years ago14
74848f8dJose Arriaga Maldonado2 years ago15string prompt = "The concept for a living room that blends Scandinavian simplicity with Japanese minimalism for"
16+ " a serene and cozy atmosphere. It's a space that invites relaxation and mindfulness, with natural light"
17+ " and fresh air. Using neutral tones, including colors like white, beige, gray, and black, that create a"
18+ " sense of harmony. Featuring sleek wood furniture with clean lines and subtle curves to add warmth and"
19+ " elegance. Plants and flowers in ceramic pots adding color and life to a space. They can serve as focal"
20+ " points, creating a connection with nature. Soft textiles and cushions in organic fabrics adding comfort"
21+ " and softness to a space. They can serve as accents, adding contrast and texture.";
9f9f2936Jose Arriaga Maldonado2 years ago22
74848f8dJose Arriaga Maldonado2 years ago23ImageGenerationOptions options = new()
24{
25Quality = GeneratedImageQuality.High,
26Size = GeneratedImageSize.W1792xH1024,
27Style = GeneratedImageStyle.Vivid,
28ResponseFormat = GeneratedImageFormat.Bytes
29};
9f9f2936Jose Arriaga Maldonado2 years ago30
74848f8dJose Arriaga Maldonado2 years ago31GeneratedImage image = client.GenerateImage(prompt, options);
32BinaryData bytes = image.ImageBytes;
9f9f2936Jose Arriaga Maldonado2 years ago33
74848f8dJose Arriaga Maldonado2 years ago34using FileStream stream = File.OpenWrite($"{Guid.NewGuid()}.png");
35bytes.ToStream().CopyTo(stream);
9f9f2936Jose Arriaga Maldonado2 years ago36}
37}