Skip to content

shaihnurov/Quantumm.Image

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Quantumm.Image

NuGet

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.


Features

  • 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

Installation

Install via NuGet:

dotnet add package Quantumm.Image --version 1.1.6

Usage

  1. 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();
  2. Resolve and use IImageCacheService
     using 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");
  3. Resolve and use IImageDownloadService via DI
     using 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}");

API

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.

Logging

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());

Supported Platforms

  • Avalonia 11
  • .NET 9.0

License

MIT License. See LICENSE for details.

Contributors

See all contributors

About

Library for storing images in the Avalonia cache

Resources

License

Stars

Watchers

Forks

Languages