Class ImageService
Implementation of IImageService based on the ImageSharp library. Handles thumbnail creation, retrieval, and deletion.
Implements
Inherited Members
Namespace: DataWeb.Media.ImageSharp
Assembly: DataWeb.Media.ImageSharp.dll
Syntax
public class ImageService : IImageService
Constructors
ImageService(IFileStorageService, IThumbnailStorageStore, IListService)
Implementation of IImageService based on the ImageSharp library. Handles thumbnail creation, retrieval, and deletion.
Declaration
public ImageService(IFileStorageService fileStorageService, IThumbnailStorageStore thumbnailStorageService, IListService listService)
Parameters
| Type | Name | Description |
|---|---|---|
| IFileStorageService | fileStorageService | |
| IThumbnailStorageStore | thumbnailStorageService | |
| IListService | listService |
Properties
Name
Identifies the image service implementation.
Declaration
public string Name { get; }
Property Value
| Type | Description |
|---|---|
| string |
Methods
CreateThumbnail(IFile, ThumbnailDefinition, int, int, ResizeMode, bool, bool, string, Func<object, Task>, CancellationToken)
Generates and saves a thumbnail for the specified file. Applies optional cropping, resizing, metadata removal, and custom processing. Vector formats (SVG/SVGZ) are stored as-is without raster processing.
Declaration
public Task CreateThumbnail(IFile file, ThumbnailDefinition thumbnailDefinition, int width, int height, ResizeMode resizeMode = ResizeMode.Fix, bool isMetaDataRemoval = false, bool isCutDisabled = false, string imageContext = null, Func<object, Task> imageProcessDelegate = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IFile | file | The source file to generate the thumbnail from. |
| ThumbnailDefinition | thumbnailDefinition | Thumbnail format and options definition. |
| int | width | Desired width in pixels. |
| int | height | Desired height in pixels. |
| ResizeMode | resizeMode | |
| bool | isMetaDataRemoval | If true, EXIF and XMP metadata are stripped from the image. |
| bool | isCutDisabled | If true, crop settings defined in the thumbnail are ignored. |
| string | imageContext | Optional context string to differentiate the thumbnail. |
| Func<object, Task> | imageProcessDelegate | Optional delegate for custom processing on the image before saving. |
| CancellationToken | cancellationToken | Cancellation token. |
Returns
| Type | Description |
|---|---|
| Task |
DeleteThumbnailsAsync(CancellationToken)
Deletes all thumbnails from storage.
Declaration
public Task DeleteThumbnailsAsync(CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken | Cancellation token. |
Returns
| Type | Description |
|---|---|
| Task |
GetImageFormatAsync(Stream, CancellationToken)
Detects the image format of the provided stream.
Declaration
public Task<string> GetImageFormatAsync(Stream stream, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| Stream | stream | The image stream to inspect. |
| CancellationToken | cancellationToken | Cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<string> | The format name in lowercase (e.g. |
GetThumbnailDefinition(IFile, bool)
Builds the appropriate ThumbnailDefinition for the specified file, determining format, extension, quality mode, and crop options. Vector formats (SVG/SVGZ) are flagged as IsVectorFormat; all others default to WebP unless overridden by file settings.
Declaration
public ThumbnailDefinition GetThumbnailDefinition(IFile file, bool isContextOriginalFormat = false)
Parameters
| Type | Name | Description |
|---|---|---|
| IFile | file | The file to build the definition for. |
| bool | isContextOriginalFormat | If true, forces the original file format regardless of settings. |
Returns
| Type | Description |
|---|---|
| ThumbnailDefinition | The configured ThumbnailDefinition. |
GetThumbnailStreamAsync(IFile, ThumbnailDefinition, int, int, ResizeMode, bool, string, CancellationToken)
Returns the stream of an already-generated thumbnail for the specified file.
Declaration
public Task<Stream> GetThumbnailStreamAsync(IFile file, ThumbnailDefinition thumbnailDefinition, int width, int height, ResizeMode resizeMode = ResizeMode.Fix, bool isCutDisabled = false, string imageContext = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IFile | file | The source file. |
| ThumbnailDefinition | thumbnailDefinition | Thumbnail format and options definition. |
| int | width | Thumbnail width in pixels. |
| int | height | Thumbnail height in pixels. |
| ResizeMode | resizeMode | Resize mode to apply. |
| bool | isCutDisabled | If true, crop settings are ignored. |
| string | imageContext | Optional context string to differentiate the thumbnail. |
| CancellationToken | cancellationToken | Cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<Stream> | The thumbnail stream. |
IsThumbnailAsync(IFile, ThumbnailDefinition, int, int, ResizeMode, bool, string, CancellationToken)
Determines whether an up-to-date thumbnail already exists for the specified file.
Declaration
public Task<bool> IsThumbnailAsync(IFile file, ThumbnailDefinition thumbnailDefinition, int width, int height, ResizeMode resizeMode = ResizeMode.Fix, bool isCutDisabled = false, string imageContext = null, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| IFile | file | The source file to check the thumbnail for. |
| ThumbnailDefinition | thumbnailDefinition | Thumbnail format and options definition. |
| int | width | Desired width in pixels. |
| int | height | Desired height in pixels. |
| ResizeMode | resizeMode | Resize mode to apply. |
| bool | isCutDisabled | If true, crop settings are ignored. |
| string | imageContext | Optional context string to differentiate the thumbnail. |
| CancellationToken | cancellationToken | Cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<bool> | true if the thumbnail exists and is up to date with the source file. |
IsValidThumbnailResolutionAsync(int, int, CancellationToken)
Checks whether the specified resolution is among the allowed resolutions defined in List_Thumbnail_AllowedResolutions.
Declaration
public Task<bool> IsValidThumbnailResolutionAsync(int width, int height, CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| int | width | Width to validate. |
| int | height | Height to validate. |
| CancellationToken | cancellationToken | Cancellation token. |
Returns
| Type | Description |
|---|---|
| Task<bool> | true if the |