ImageDecoder Class

Decodes the frames of an encoded image (https://w3c.github.io/webcodecs/#imagedecoder) with the image decoders of the rendering backend (BrowsingContextOptions.RenderingBackend), the ones of the img elements: PNG (including the animated PNG), JPEG, GIF, WebP, AVIF, JPEG XL, BMP and ICO with the backend of Meziantou.Framework.WebPlatform.Imaging. The data is a buffer, or a stream whose frames can be decoded as they arrive.

public sealed class ImageDecoder

Remarks

The decoding runs in tasks of the event loop of the global object, in the order of the calls of ImageDecoder.Decode. An image has one track: the frames of an animation, or its still image (an AVIF image that has both a still image and an image sequence has the track of the sequence). The frames are RGBA; their rotation and flip are the EXIF orientation of the image.

The frame count of a stream is the number of frames whose data is complete: the frames of a GIF and of an animated WebP image are counted as they arrive, the other formats are parsed again when a chunk arrives.

See also

Constructors

NameDescription
ImageDecoder(GlobalScope, ImageDecoderInit)Creates a decoder whose tasks run on the event loop of a global object (https://w3c.github.io/webcodecs/#dom-imagedecoder-imagedecoder).
ImageDecoder(ImageDecoderInit)Creates a decoder (https://w3c.github.io/webcodecs/#dom-imagedecoder-imagedecoder).

ImageDecoder overloads

ImageDecoder(GlobalScope, ImageDecoderInit)

ImageDecoder.cs

Creates a decoder whose tasks run on the event loop of a global object (https://w3c.github.io/webcodecs/#dom-imagedecoder-imagedecoder).

public ImageDecoder(GlobalScope global, ImageDecoderInit init)
Parameters
global GlobalScope
The global object (a Window or a worker global scope): its rendering backend decodes the image, and the tasks of the decoder run on its event loop.
init ImageDecoderInit
The type and the data of the image (a byte[] or a ReadOnlyMemory<T> of bytes), and the decoding options.
Exceptions
TypeErrorException
The type is not an image MIME type, the data is empty, or only one of the desired width and height is given.

ImageDecoder(ImageDecoderInit)

ImageDecoder.cs

Creates a decoder (https://w3c.github.io/webcodecs/#dom-imagedecoder-imagedecoder).

public ImageDecoder(ImageDecoderInit init)
Parameters
init ImageDecoderInit
The type and the data of the image, and the decoding options.
Exceptions
TypeErrorException
The type is not an image MIME type, the data is empty, detached, or a locked or disturbed stream, or only one of the desired width and height is given.
See also

Properties

NameDescription
CompleteGets whether all the data of the image has been received.
CompletedGets a task that completes when all the data of the image has been received, or fails when the decoder is closed before.
TracksGets the tracks of the image.
TypeGets the MIME type given to the constructor.

Gets whether all the data of the image has been received.

public bool Complete { get; }

Gets a task that completes when all the data of the image has been received, or fails when the decoder is closed before.

public Task Completed { get; }

Gets the tracks of the image.

public ImageTrackList Tracks { get; }

Gets the MIME type given to the constructor.

public string Type { get; }

Methods

NameDescription
Close()Aborts the pending decode requests and releases the resources of the decoder (https://w3c.github.io/webcodecs/#dom-imagedecoder-close): the decoder cannot be used anymore.
Decode(ImageDecodeOptions?)Decodes a frame of the selected track (https://w3c.github.io/webcodecs/#dom-imagedecoder-decode).
IsTypeSupported(string)Returns whether a type is a MIME type of an image the decoders support (https://w3c.github.io/webcodecs/#dom-imagedecoder-istypesupported).
Reset()Aborts the pending decode requests with an AbortError (https://w3c.github.io/webcodecs/#dom-imagedecoder-reset).

Aborts the pending decode requests and releases the resources of the decoder (https://w3c.github.io/webcodecs/#dom-imagedecoder-close): the decoder cannot be used anymore.

public void Close()

Decode(ImageDecodeOptions?)

ImageDecoder.cs

Decodes a frame of the selected track (https://w3c.github.io/webcodecs/#dom-imagedecoder-decode).

public Task<ImageDecodeResult> Decode(ImageDecodeOptions? options = null)

Parameters

options ImageDecodeOptions?
The index of the frame.

Returns

Task<ImageDecodeResult> A task that completes with the frame once its data has been received and decoded, or fails with an InvalidStateError when the decoder is closed or no track is selected, a RangeErrorException when the image has no such frame, an EncodingError when the data is corrupted, or an AbortError when the decoder is reset or closed before.

See also

IsTypeSupported(string)

ImageDecoder.cs

Returns whether a type is a MIME type of an image the decoders support (https://w3c.github.io/webcodecs/#dom-imagedecoder-istypesupported).

public static Task<bool> IsTypeSupported(string type)

Parameters

type string

Returns

Task<bool> A task that completes in a task of the event loop, or fails with a TypeError when the type is not a valid image MIME type.

See also

Aborts the pending decode requests with an AbortError (https://w3c.github.io/webcodecs/#dom-imagedecoder-reset).

public void Reset()