Quantumm.Image is an asynchronous, thread-safe, LRU-aware image caching and downloading library designed for Avalonia applications. It provides fast and memory-efficient image loading and downloading with support for dependency injection and duplicate download prevention.
- Asynchronous image loading from disk
- Asynchronous image downloading from a URL to a user-specified folder
- Memory-limited LRU (Least Recently Used) caching
- Thread-safe access to cached images
- Automatic eviction of least recently used images
- Prevents multiple concurrent loads of the same image
- Easy dependency injection integration with
Microsoft.Extensions.DependencyInjection - Optional logging via
Microsoft.Extensions.Logging - Detailed XML documentation for all public interfaces
- Supports Avalonia 11 and .NET 9
Install via NuGet:
dotnet add package Quantumm.Image --version 1.1.6- Register the service in your DI container
using Microsoft.Extensions.DependencyInjection; using Quantumm.Image.Services.DependencyInjection; var services = new ServiceCollection(); // Register ImageCacheService with optional capacity (default 100) services.AddImageCache(capacity: 200); // Register ImageDownloadService services.AddHttpClient<IImageDownloadService, ImageDownloadService>(); var serviceProvider = services.BuildServiceProvider();
- Resolve and use
IImageCacheServiceusing Quantumm.Image.Services.Cache; using Avalonia.Media.Imaging; var imageCache = serviceProvider.GetRequiredService<IImageCacheService>(); // Load an image asynchronously Bitmap image = await imageCache.LoadImage("path/to/image.png"); // Update an image in cache if the file has changed Bitmap updatedImage = await imageCache.UpdateImage("path/to/image.png");
- Resolve and use
IImageDownloadServicevia DIusing Quantumm.Image.Services.Downloader; var imageDownloader = serviceProvider.GetRequiredService<IImageDownloadService>(); // Download an image to a folder string savedPath = await imageDownloader.DownloadImageAsync("https://example.com/image.png", "image.png", "C:\\Images"); Console.WriteLine($"Image saved to: {savedPath}");
IImageCacheService
-
Task<Bitmap> LoadImage(string filePath)Loads an image from cache or disk asynchronously. Adds it to the cache if it wasn't already present. -
Task<Bitmap> UpdateImage(string path)Updates an existing image in the cache by loading a new version from disk. Adds it if it does not exist.
IImageDownloadService
Task<string> DownloadImageAsync(string? url, string fileName, string folderPath, CancellationToken cancellationToken = default)Downloads an image from a given URL and saves it to the specified folder. Throws exceptions on failure. Logs if a logger is provided.
Both ImageCacheService and ImageDownloadService support optional logging via Microsoft.Extensions.Logging. If a logger is provided, the following events are logged:
- Image loading and updating
- Cache eviction
- Download start, completion, and errors
- Errors during file system operations
Example:
services.AddLogging(builder => builder.AddConsole());- Avalonia 11
- .NET 9.0
MIT License. See LICENSE for details.