Easy to use SDK for high-fidelity image generation and editing using OpenAi's new gpt-image-1 model
$ dotnet add package ImageGenAiAI-Powered Image Editing for .NET Developers
Transform and create images with OpenAI's new gpt-image-1 model. Generate, edit, and enhance images using simple C# code. Perfect for web apps, APIs, and automation tools.
When creating high quality and high fidelity images the API can take up to 60+ seconds to respond
Your imagination is the limit!
dotnet add package ImageGenAi
Important: You need to make sure your API key has access to the gpt-image-1 model or this will fail. Get your API key from https://platform.openai.com/api-keys
// In Program.cs
using ImageGen.Configuration;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddImageGenClient(options =>
{
options.ApiKey = builder.Configuration["OPENAI_API_KEY"]!;
// Api key from appsettings.json
});
var app = builder.Build();
Some examples of how to get an image stream into ImageGen.
From a file on disk:
using var fileStream = new FileStream("path/to/image.jpg", FileMode.Open);
From an uploaded file (ASP.NET Core):
// In your controller or page handler
public async Task<IActionResult> UploadImage(IFormFile uploadedFile)
{
using var stream = uploadedFile.OpenReadStream();
// Now use the stream with ImageGen
}
From a URL:
using var httpClient = new HttpClient();
using var stream = await httpClient.GetStreamAsync("https://example.com/image.jpg");
From a byte array:
var imageBytes = File.ReadAllBytes("path/to/image.jpg");
using var stream = new MemoryStream(imageBytes);
using ImageGen.Core;
using ImageGen.Models;
// Inject the client
public class ImageService(IImageGenClient client)
{
public async Task<byte[]> RemoveBackground(Stream imageStream)
{
var result = await client.EditAsync(new EditRequest(
PrimaryImage: imageStream,
Prompt: "Remove the background completely",
InputFidelity: InputFidelity.High
));
return result.Bytes.ToArray();
}
}
public async Task<byte[]> RemoveBackground(Stream imageStream)
{
var result = await client.EditAsync(new EditRequest(
PrimaryImage: imageStream,
Prompt: "Remove the background, make it transparent",
InputFidelity: InputFidelity.High, // Keeps details crisp
Format: ImageFormat.Png
));
return result.Bytes.ToArray();
}
public async Task<byte[]> ChangeBackground(Stream productImage)
{
var result = await client.EditAsync(new EditRequest(
PrimaryImage: productImage,
Prompt: "Place this product on a luxury marble background",
InputFidelity: InputFidelity.High,
Quality: ImageQuality.High
));
return result.Bytes.ToArray();
}
public async Task<byte[]> AddLogo(Stream mainImage, Stream logoImage)
{
var result = await client.EditAsync(new EditRequest(
PrimaryImage: mainImage,
SecondaryImages: new[] { logoImage },
Prompt: "Add the logo in the bottom right corner, blend naturally",
InputFidelity: InputFidelity.High
));
return result.Bytes.ToArray();
}
public async Task<byte[]> GenerateImage(string description)
{
var result = await client.GenerateAsync(new GenerateRequest(
Prompt: $"Professional product photo: {description}",
Width: 1024,
Height: 1024,
Quality: ImageQuality.High,
Format: ImageFormat.Png
));
return result.Bytes.ToArray();
}
To save images to disk, simply write the bytes to a file:
// Save any result to disk
await File.WriteAllBytesAsync("output.png", result.Bytes.ToArray());
// Or with automatic file extension based on format
var extension = result.Format switch {
ImageFormat.Png => "png",
ImageFormat.Jpeg => "jpg",
ImageFormat.Webp => "webp"
};
await File.WriteAllBytesAsync($"output.{extension}", result.Bytes.ToArray());
InputFidelity.High preserves faces, logos, and detailsGenerateAsync() - Create images from text promptsEditAsync() - Edit existing images with promptsWant to see it in action? Check out the simple web demo:
Each example shows you the exact AI prompt being used, so you can learn and adapt them for your own projects!
ImageGenException, RateLimitExceededExceptionInputFidelity.High for important editsMIT License - see LICENSE file for details.
Ready to supercharge your .NET apps with AI image editing? 🚀
Start with the demo app, then integrate ImageGenAI into your project today!