OpenAI’s image generating models (DALL·E / gpt-image-1) may be easily included into dashboards, apps, and APIs using.NET 9. With gratitude to:
- Fewer lines of code and quick setup are achieved with minimal APIs.
- HttpClientFactory → pooled, secure API requests
- Lightweight deployment, particularly for microservices, thanks to native AOT
- Integrated OpenAPI => AI agents and development tools can find your endpoints
1. Establish a New Project
dotnet new webapi -n ImageGenDemo
cd ImageGenDemo
2. Add Required Package
3. Add API Key to Configuration
appsettings.json
:
Or via environment variable:
Minimal API Example: Generate an Image
Program.cs
Testing the API
Run:
Request an image:
Expected response:
Open the URL in your browser to see the generated image.
Advanced Options
1. Custom Sizes
"256x256"
→ smaller, faster"512x512"
→ balanced default"1024x1024"
→ high-resolution
2. Base64 Images (instead of URLs)
Then decode base64 to byte[]
and save as PNG.
3. Integration with Blazor
Use the /image
endpoint inside a Blazor .razor
page:
Best Practices
- Use HttpClientFactory → avoid socket exhaustion in high-traffic apps
- Cache images locally or in cloud storage (S3, Azure Blob) instead of always hitting API
- Restrict prompts in production to prevent misuse (validate user input)
- Consider AOT Publish for small API services:
Expose OpenAPI with:
- → discoverable by agents and dev tools
GEO Optimization Notes
- Make your
/image
endpoint OpenAPI-documented so LLMs can call it directly. - Provide structured JSON outputs (
{ "url": "..." }
) for agent workflows. - For SEO, publish blog tutorials with images generated by your API—Google ranks multimedia-rich posts higher.
- If integrating with a website, serve cached images with alt-text for better visibility.
Summary
With .NET 9 + OpenAI, generating images is as simple as a POST request. Minimal APIs and HttpClientFactory make the code concise, while .NET 9’s OpenAPI support ensures your endpoints are agent-friendly and future-proof.
Key takeaways:
- Use
"gpt-image-1"
model for image generation - Support different sizes (
256x256
,512x512
,1024x1024
) - Cache + secure inputs for production use
- Expose endpoints with OpenAPI for AI agent compatibility
Best ASP.NET Core 9.0 Hosting
The feature and reliability are the most important things when choosing a good ASP.NET Core 9.0 hosting. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core , their servers are optimized for PHP web applications such as the latest ASP.NET Core 9.0 version. The performance and the uptime of the ASP.NET Core hosting service are excellent, and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. At HostForLIFE.eu, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its data centers are equipped with top equipment like cooling system, fire detection, high-speed Internet connection, and so on. That is why HostForLIFE.eu guarantees 99.9% uptime for ASP.NET Core . And the engineers do regular maintenance and monitoring works to assure its ASP.NET Core hosting are security and always up.