Image Generation
Overview
LlmTornado provides powerful image generation capabilities through multiple AI providers including OpenAI (DALL-E), Google (Imagen), and X.AI (Grok). You can generate, edit, and manipulate images using natural language prompts. The library supports various image formats, qualities, and styles to suit different use cases.
Quick Start
Here's a simple image generation example:
csharp
using LlmTornado;
using LlmTornado.Images;
using LlmTornado.Images.Models;
TornadoApi api = new TornadoApi("your-api-key");
// Generate an image
ImageGenerationResult? result = await api.ImageGenerations.CreateImage(
new ImageGenerationRequest("a cute cat sitting on a sunny windowsill")
{
Quality = TornadoImageQualities.Hd,
ResponseFormat = TornadoImageResponseFormats.Url,
Model = ImageModel.OpenAi.Dalle.V3
});
Console.WriteLine(result?.Data?[0].Url);Prerequisites
Before using image generation features, ensure you have:
- The LlmTornado package installed
- A valid API key with image generation access
- Understanding of image generation concepts and prompts
- Sufficient API credits (image generation can be expensive)
Detailed Explanation
Image Generation Models
LlmTornado supports multiple image generation models:
OpenAI Models
- DALL-E 3 - High-quality, detailed images with excellent prompt following
- DALL-E 2 - Fast generation with good quality
- GPT-1 (Image) - Latest generation model with advanced features
Google Models
- Imagen 3 - Google's latest image generation model
- Imagen 4 Preview - Preview of next-generation features
X.AI Models
- Grok 2 - X.AI's image generation model
Response Formats
Images can be returned in two formats:
- URL - Direct link to the generated image (expires after a period)
- Base64 - Encoded image data for immediate use or storage
Quality Options
Control the output quality:
- Standard - Fast generation, good quality
- HD - Higher quality, more detailed images
- Medium - Balanced quality and speed
Basic Usage
Generate with DALL-E 3
csharp
ImageGenerationResult? result = await api.ImageGenerations.CreateImage(
new ImageGenerationRequest("a cute cat",
quality: TornadoImageQualities.Hd,
responseFormat: TornadoImageResponseFormats.Url,
model: ImageModel.OpenAi.Dalle.V3));
Console.WriteLine($"Image URL: {result?.Data?[0].Url}");Generate with Base64 Response
csharp
ImageGenerationResult? result = await api.ImageGenerations.CreateImage(
new ImageGenerationRequest("a cute cat",
quality: TornadoImageQualities.Hd,
responseFormat: TornadoImageResponseFormats.Base64,
model: ImageModel.OpenAi.Dalle.V3));
if (result?.Data?[0].Base64 != null)
{
byte[] imageBytes = Convert.FromBase64String(result.Data[0].Base64);
await File.WriteAllBytesAsync("generated_image.jpg", imageBytes);
}Generate with GPT-1
csharp
ImageGenerationResult? result = await api.ImageGenerations.CreateImage(
new ImageGenerationRequest("a cute cat",
quality: TornadoImageQualities.Medium,
model: ImageModel.OpenAi.Gpt.V1)
{
Background = ImageBackgroundTypes.Transparent,
Moderation = ImageModerationTypes.Low
});
byte[] imageBytes = Convert.FromBase64String(result.Data[0].Base64);
await File.WriteAllBytesAsync("cat_transparent.png", imageBytes);Generate with Google Imagen
csharp
ImageGenerationResult? result = await api.ImageGenerations.CreateImage(
new ImageGenerationRequest("a cute cat",
responseFormat: TornadoImageResponseFormats.Base64,
model: ImageModel.Google.Imagen.V3Generate002)
{
VendorExtensions = new ImageGenerationRequestVendorExtensions(
new ImageGenerationRequestGoogleExtensions
{
MimeType = ImageGenerationRequestGoogleExtensionsMimeTypes.Jpeg,
CompressionQuality = 90
})
});
byte[] imageBytes = Convert.FromBase64String(result.Data[0].Base64);
await File.WriteAllBytesAsync("cat_imagen.jpg", imageBytes);Generate with Grok 2
csharp
ImageGenerationResult? result = await api.ImageGenerations.CreateImage(
new ImageGenerationRequest
{
Prompt = "a cute cat in a cyberpunk setting",
ResponseFormat = TornadoImageResponseFormats.Base64,
Model = ImageModel.XAi.Grok.V2241212
});
byte[] imageBytes = Convert.FromBase64String(result.Data[0].Base64);
await File.WriteAllBytesAsync("cyberpunk_cat.jpg", imageBytes);Advanced Usage
Image Editing
Edit existing images with prompts:
csharp
// First, generate an image
ImageGenerationResult? original = await api.ImageGenerations.CreateImage(
new ImageGenerationRequest("a cute cat",
quality: TornadoImageQualities.Medium,
model: ImageModel.OpenAi.Gpt.V1)
{
Background = ImageBackgroundTypes.Transparent,
Moderation = ImageModerationTypes.Low
});
// Then edit it
ImageGenerationResult? edited = await api.ImageEdit.EditImage(
new ImageEditRequest("make this cat look more dangerous")
{
Quality = TornadoImageQualities.Medium,
Model = ImageModel.OpenAi.Gpt.V1,
Image = new TornadoInputFile(original.Data[0].Base64, "image/png")
});
byte[] editedBytes = Convert.FromBase64String(edited.Data[0].Base64);
await File.WriteAllBytesAsync("dangerous_cat.png", editedBytes);Advanced Imagen 4 Generation
Use Google's Imagen 4 preview with advanced settings:
csharp
ImageGenerationResult? result = await api.ImageGenerations.CreateImage(
new ImageGenerationRequest("a cute cat",
responseFormat: TornadoImageResponseFormats.Base64,
model: ImageModel.Google.ImagenPreview.V4Preview250606)
{
VendorExtensions = new ImageGenerationRequestVendorExtensions(
new ImageGenerationRequestGoogleExtensions
{
MimeType = ImageGenerationRequestGoogleExtensionsMimeTypes.Jpeg,
CompressionQuality = 90,
AspectRatio = "16:9", // Widescreen format
NegativePrompt = "blurry, low quality"
})
});
byte[] imageBytes = Convert.FromBase64String(result.Data[0].Base64);
await File.WriteAllBytesAsync("cat_widescreen.jpg", imageBytes);Batch Image Generation
Generate multiple images at once:
csharp
string[] prompts = [
"a cute cat in a garden",
"a playful kitten with a ball of yarn",
"an elegant cat on a velvet cushion"
];
List<ImageGenerationResult?> results = [];
foreach (string prompt in prompts)
{
ImageGenerationResult? result = await api.ImageGenerations.CreateImage(
new ImageGenerationRequest(prompt,
quality: TornadoImageQualities.Hd,
model: ImageModel.OpenAi.Dalle.V3));
results.Add(result);
Console.WriteLine($"Generated: {prompt} -> {result?.Data?[0].Url}");
// Respect rate limits
await Task.Delay(1000);
}Saving and Displaying Images
Helper method to save generated images:
csharp
async Task SaveGeneratedImage(ImageGenerationResult? result, string filename)
{
if (result?.Data?[0] != null)
{
if (!string.IsNullOrEmpty(result.Data[0].Base64))
{
byte[] imageBytes = Convert.FromBase64String(result.Data[0].Base64);
await File.WriteAllBytesAsync(filename, imageBytes);
Console.WriteLine($"Image saved to {filename}");
}
else if (!string.IsNullOrEmpty(result.Data[0].Url))
{
using HttpClient client = new HttpClient();
byte[] imageBytes = await client.GetByteArrayAsync(result.Data[0].Url);
await File.WriteAllBytesAsync(filename, imageBytes);
Console.WriteLine($"Image downloaded and saved to {filename}");
}
}
}
// Usage
ImageGenerationResult? result = await api.ImageGenerations.CreateImage(
new ImageGenerationRequest("a sunset over mountains"));
await SaveGeneratedImage(result, "sunset.jpg");Error Handling
Robust error handling for image generation:
csharp
try
{
ImageGenerationResult? result = await api.ImageGenerations.CreateImage(
new ImageGenerationRequest("a cute cat")
{
Quality = TornadoImageQualities.Hd,
Model = ImageModel.OpenAi.Dalle.V3
});
if (result?.Data?.Count > 0)
{
Console.WriteLine("Image generated successfully!");
}
else
{
Console.WriteLine("No image data returned");
}
}
catch (Exception ex)
{
Console.WriteLine($"Error generating image: {ex.Message}");
// Handle specific errors
if (ex.Message.Contains("rate limit"))
{
Console.WriteLine("Rate limit exceeded. Please wait and try again.");
}
else if (ex.Message.Contains("content policy"))
{
Console.WriteLine("Prompt violates content policy. Please modify your prompt.");
}
}Best Practices
1. Write Effective Prompts
- Be specific and descriptive
- Include style, mood, and composition details
- Mention lighting, colors, and perspective
- Use negative prompts to avoid unwanted elements (where supported)
2. Manage Costs
- Use standard quality for testing
- Use HD quality only when necessary
- Cache and reuse generated images
- Consider cheaper models for simple images
3. Handle Rate Limits
- Implement delays between requests
- Use exponential backoff for retries
- Monitor your API usage
- Queue requests for batch processing
4. Store Images Efficiently
- Save base64 images to disk/database immediately
- Download URL images before they expire
- Compress images for storage if quality allows
- Use appropriate image formats (JPEG vs PNG)
5. Content Moderation
- Validate prompts before sending
- Handle content policy violations gracefully
- Use moderation settings appropriately
- Implement user prompt filtering
Common Issues
Content Policy Violation
- Issue: Prompt rejected by content filter
- Solution: Modify prompt to remove problematic content
- Prevention: Validate prompts against policy guidelines
Rate Limit Exceeded
- Issue: Too many generation requests
- Solution: Implement rate limiting and queuing
- Prevention: Monitor usage and add delays between requests
Image Quality Issues
- Issue: Generated images don't match expectations
- Solution: Refine prompts with more specific details
- Prevention: Test prompts and iterate on descriptions
Expired URLs
- Issue: Image URLs no longer accessible
- Solution: Download and store images immediately
- Prevention: Use base64 format or download URLs promptly
API Reference
ImageGenerationRequest
string Prompt- Description of image to generateImageModel Model- Model to use for generationTornadoImageQualities Quality- Output quality levelTornadoImageResponseFormats ResponseFormat- URL or Base64ImageBackgroundTypes Background- Background type (transparent, opaque)ImageModerationTypes Moderation- Content moderation levelImageGenerationRequestVendorExtensions VendorExtensions- Provider-specific options
ImageGenerationResult
List<ImageData> Data- Generated image dataint Created- Timestamp of creationstring Model- Model used
ImageData
string Url- Direct image URL (if requested)string Base64- Base64 encoded image (if requested)string RevisedPrompt- AI-revised prompt (some models)
ImageModel
OpenAi.Dalle.V3- DALL-E 3 modelOpenAi.Dalle.V2- DALL-E 2 modelOpenAi.Gpt.V1- GPT image modelGoogle.Imagen.V3Generate002- Imagen 3Google.ImagenPreview.V4Preview250606- Imagen 4 PreviewXAi.Grok.V2241212- Grok 2 image model
Related Topics
- Vision - Image understanding and analysis
- Files - File management
- Chat Basics - Core chat functionality