July 2nd, 2025
like3 reactions

Local AI + .NET = AltText Magic in One C# Script

Bruno Capuano
Cloud Advocate

Need to generate image descriptions fast? In this post, we’ll show how to combine .NET 10’s new capabilities with local AI models to create smart AltText — all in one simple C# file. It’s a fun way to explore what AI can do beyond chat.

AltText Generation with Local AI Models and dotnet run app.cs

✨ Intro: Let’s Talk About AltText

Accessibility matters — and one simple but powerful way to improve it is by adding AltText to images. Alternative text helps screen readers describe images to visually impaired users, improves SEO, and enhances overall UX. But writing descriptive alt text for every image can be repetitive. That’s where AI comes in!

🤖 Using Local Models with Ollama

Local models are a game changer. No rate limits, no cloud latency, and full control over the models you use.

In this sample, we will use Ollama to run a vision model like gemma3, llama3.2-vision, or mistral-small3.2. These models are great at understanding image content and generating rich natural language descriptions.

ollama run gemma3 
# or ollama run llama3.2-vision 
# or ollama run mistral-small3.2

Once Ollama is running locally (usually on http://localhost:11434), you can send requests to analyze an image and receive a natural language description.

Coming Soon

AI Foundry Local will provide similar capabilities with local models. Stay tuned!

🚀 Run C# as a Script with dotnet run app.cs

.NET 10 introduced a cool new feature: you can now run a C# file directly with dotnet run. No project scaffolding, no .csproj files — just clean, script-like execution.

Ref: https://devblogs.microsoft.com/dotnet/announcing-dotnet-run-app/

dotnet run app.cs

This is incredibly convenient for scripting tasks like image processing, quick automation, or dev tooling.

Let’s see it in action!

📂 The Full Code Sample

✨ Source: https://gist.github.com/elbruno/4396c9ee3e56d1c86d280faa33b8f9fe

Save this code as alttext.cs and run it using dotnet run alttext.cs <your image path>. Make sure Ollama is running and the image path is correct.

// alttext.cs
#:package OllamaSharp@5.1.19

using OllamaSharp;

// set up the client
var uri = new Uri("http://localhost:11434");
var ollama = new OllamaApiClient(uri);
ollama.SelectedModel = "gemma3";
var chat = new Chat(ollama);

// read the image file from arguments
byte[] imageBytes = File.ReadAllBytes(args[0]);
var imageBytesEnumerable = new List<IEnumerable<byte>> { imageBytes };

// generate the alt text
var message = "Generate a complete alt text description for the attached image. The description should be detailed and suitable for visually impaired users. Do not include any information about the image file name or format.";
await foreach (var answerToken in chat.SendAsync(message: message, imagesAsBytes: imageBytesEnumerable))
    Console.Write(answerToken);

// done
Console.WriteLine($">> Ollama done");;

Screenshot showing Visual Studio Code terminal with AltText generation results from analyzing an image

✉ Tip: If your image is large, consider resizing it before encoding. This reduces request size and speeds up inference.

🔮 What’s Next?

With .NET 10 and the power of local AI models like Azure AI Foundry Local Ollama, we’re no longer limited to “chat with AI” scenarios. We can now:

  • Analyze media
  • Automate content generation
  • Build offline-capable AI features

This is also a great way to learn! Try switching to different local models, modifying the prompt, or adding a few lines to copy the result to your clipboard. These small tweaks help you experiment and understand how to integrate AI into real-world apps.

To go further:

.NET keeps getting more fun, and AI keeps getting more powerful — especially when you run it locally ✨

Have fun generating smart AltText and making your apps more accessible!


📃 Resources

Author

Bruno Capuano
Cloud Advocate

0 comments