Table of Contents

Class ImageCache

Namespace
McServerLauncher.Services
Assembly
McServerLauncher.dll

Remote images (project icons and gallery screenshots) with a memory + disk cache in front.

The URLs come from Modrinth's API, but they are still remote input, so every download is guarded: the response has to declare an image content-type and stay under a byte cap that is enforced while streaming, so a missing or lying Content-Length can't balloon memory either. Anything odd yields null and the caller keeps its placeholder.

Decoding happens on the calling (background) thread, never on the UI thread. Bitmaps are cached and handed to several views at once, so they are deliberately never disposed here — a disposed bitmap still bound to an Image would throw when it is drawn.

public static class ImageCache
Inheritance
ImageCache
Inherited Members

Fields

MaxGalleryBytes

Cap for gallery screenshots, which are full-size images rather than icons.

public const int MaxGalleryBytes = 8388608

Field Value

int

MaxIconBytes

Generous cap for a project icon (they're typically a few KB).

public const int MaxIconBytes = 2097152

Field Value

int

Methods

GetAsync(string?, int, CancellationToken)

Returns the decoded image for url, from memory, then disk, then the network. Concurrent callers for the same URL share one download.

Returns null for everything that isn't an image in hand — an empty URL, a rejected download, an undecodable format, or a caller that cancelled. Callers all do the same thing with that answer (keep the placeholder), and since most of them are fire-and-forget, a thrown cancellation would only become an unobserved task exception.

public static Task<Bitmap?> GetAsync(string? url, int maxBytes = 2097152, CancellationToken ct = default)

Parameters

url string
maxBytes int
ct CancellationToken

Returns

Task<Bitmap>