Show / Hide Table of Contents

Class SKAnimatedImage

Represents an animated image that can be decoded and presented frame by frame.

Inheritance
object
SKAnimatedImage
Implements
IDisposable
IAsyncDisposable
Inherited Members
object.GetType()
object.ToString()
object.Equals(object)
object.Equals(object, object)
object.ReferenceEquals(object, object)
object.GetHashCode()
Namespace: FFmpeg.Skia
Assembly: FFmpeg.Skia.dll
Syntax
public sealed class SKAnimatedImage : IDisposable, IAsyncDisposable
Remarks

The image supports two mutually exclusive decoding modes: manual and automatic. In manual mode, individual frames are decoded by calling DecodeNextImage() or DecodeNextImageAsync(CancellationToken).

In automatic mode, frames are decoded and presented according to their timing information after calling Start(bool).

The decoded frame is exposed through Frame and its timing information through FrameInfo.

Only one decoding or seeking operation can be active at a time.

Properties

View Source

Frame

Gets the most recently decoded frame.

Declaration
public SKBitmap Frame { get; }
Property Value
Type Description
SKBitmap

The bitmap containing the most recently decoded frame.

Remarks

The bitmap is reused internally between frames. Consumers should not dispose the returned bitmap or retain it for use after another frame has been decoded.

It may be drawn from a render loop independently of ReadyToRender: the bitmap is not written until the frame after the next one is decoded, so drawing it is safe for at least one frame duration after reading this property. Its size has square pixels (see SampleAspectRatio); draw it with Rotation.

View Source

FrameInfo

Gets the timing information of the most recently decoded frame.

Declaration
public FFCodecFrameInfo FrameInfo { get; }
Property Value
Type Description
FFCodecFrameInfo

The presentation timestamp and duration of the current frame.

View Source

Rotation

Gets the clockwise rotation in degrees (0, 90, 180 or 270) the video must be displayed with, from the stream's display matrix (e.g. a video recorded with a phone held upright).

Declaration
public int Rotation { get; }
Property Value
Type Description
int
Remarks

The decoded images are not rotated; draw them with DrawRotated(SKCanvas, SKBitmap, SKRect, int, SKSamplingOptions, SKPaint?).

View Source

SampleAspectRatio

Gets the sample (pixel) aspect ratio of the video; 1:1 for square pixels. Frame is scaled to square pixels (e.g. 720x480 with 32:27 is decoded as 853x480).

Declaration
public Rational SampleAspectRatio { get; }
Property Value
Type Description
Rational
View Source

State

Gets the current decoding state.

Declaration
public DecodingState State { get; }
Property Value
Type Description
DecodingState

The current DecodingState of the image.

Methods

View Source

Create(Stream, int, FFmpegWorkQueue?, InputFormat?, IDictionary<string, string>?, DeviceType)

Creates an SKAnimatedImage from a stream.

Declaration
public static SKAnimatedImage Create(Stream stream, int videoIndex = -1, FFmpegWorkQueue? workQueue = null, InputFormat? format = null, IDictionary<string, string>? options = null, DeviceType hwDevice = DeviceType.None)
Parameters
Type Name Description
Stream stream

The stream containing the image.

int videoIndex

The index of the video stream to decode, or -1 to automatically select the best video stream.

FFmpegWorkQueue workQueue

The optional FFmpeg.Threading.FFmpegWorkQueue used for asynchronous decoding and automatic decoding. If null, only synchronous decoding and automatic decoding without queued FFmpeg operations are available.

InputFormat? format

The optional input format to use when opening the stream.

IDictionary<string, string> options

Optional format options used when opening the source.

DeviceType hwDevice

The hardware device to use for decoding, or FFmpeg.HW.DeviceType.None to use software decoding.

Returns
Type Description
SKAnimatedImage

A new SKAnimatedImage instance.

Exceptions
Type Condition
ArgumentOutOfRangeException

The media has no video stream, or videoIndex is not a valid stream index.

View Source

Create(string, int, FFmpegWorkQueue?, InputFormat?, IDictionary<string, string>?, DeviceType)

Creates an SKAnimatedImage from a media URL.

Declaration
public static SKAnimatedImage Create(string url, int videoIndex = -1, FFmpegWorkQueue? workQueue = null, InputFormat? format = null, IDictionary<string, string>? options = null, DeviceType hwDevice = DeviceType.None)
Parameters
Type Name Description
string url

The URL or path of the media source.

int videoIndex

The index of the video stream to decode, or -1 to automatically select the best video stream.

FFmpegWorkQueue workQueue

The optional FFmpeg.Threading.FFmpegWorkQueue used for asynchronous decoding and automatic decoding. If null, only synchronous decoding and automatic decoding without queued FFmpeg operations are available.

InputFormat? format

The optional input format to use when opening the source.

IDictionary<string, string> options

Optional format options used when opening the source.

DeviceType hwDevice

The hardware device to use for decoding, or FFmpeg.HW.DeviceType.None to use software decoding.

Returns
Type Description
SKAnimatedImage

A new SKAnimatedImage instance.

Exceptions
Type Condition
ArgumentOutOfRangeException

The media has no video stream, or videoIndex is not a valid stream index.

View Source

DecodeNextImage()

Decodes the next frame of the animation.

Declaration
public bool DecodeNextImage()
Returns
Type Description
bool

true if a frame was decoded; otherwise, false if the end of the animation was reached.

Exceptions
Type Condition
ObjectDisposedException

The image has been disposed.

InvalidOperationException

Another decoding, seeking, or automatic playback operation is currently active.

View Source

DecodeNextImageAsync(CancellationToken)

Asynchronously decodes the next frame of the animation.

Declaration
public Task<bool> DecodeNextImageAsync(CancellationToken token = default)
Parameters
Type Name Description
CancellationToken token

A token used to cancel the asynchronous decoding operation.

Returns
Type Description
Task<bool>

A task that completes with true if a frame was decoded; otherwise, false if the end of the animation was reached.

Exceptions
Type Condition
ObjectDisposedException

The image has been disposed.

InvalidOperationException

An FFmpeg.Threading.FFmpegWorkQueue was not provided when the image was created, or another decoding, seeking, or automatic playback operation is currently active.

View Source

Dispose()

Releases all resources used by the animated image.

Declaration
public void Dispose()
Remarks

If automatic or asynchronous decoding is currently active, disposal cancels the operation and waits for it to finish before releasing the underlying FFmpeg resources. The bitmaps are released once a running ReadyToRender handler has returned, so Dispose may also be called from a handler.

View Source

DisposeAsync()

Releases all resources used by the animated image.

Declaration
public ValueTask DisposeAsync()
Returns
Type Description
ValueTask
Remarks

If automatic or asynchronous decoding is currently active, disposal cancels the operation and waits for it to finish before releasing the underlying FFmpeg resources. The bitmaps are released once a running ReadyToRender handler has returned.

View Source

Seek(TimeSpan)

Seeks the decoder to the specified presentation timestamp.

Declaration
public AVResult32 Seek(TimeSpan time)
Parameters
Type Name Description
TimeSpan time

The presentation timestamp to seek to.

Returns
Type Description
AVResult32

The FFmpeg result of the seek operation.

Remarks

After a successful seek, FrameInfo is reset because the previously decoded frame no longer represents the current decoder position.

Exceptions
Type Condition
ObjectDisposedException

The image has been disposed.

InvalidOperationException

Another decoding, seeking, or automatic playback operation is currently active.

View Source

SeekAsync(TimeSpan, CancellationToken)

Asynchronously seeks the decoder to the specified presentation timestamp.

Declaration
public Task<AVResult32> SeekAsync(TimeSpan time, CancellationToken token = default)
Parameters
Type Name Description
TimeSpan time

The presentation timestamp to seek to.

CancellationToken token

A token used to cancel the asynchronous seek operation.

Returns
Type Description
Task<AVResult32>

A task that completes with the FFmpeg result of the seek operation.

Exceptions
Type Condition
ObjectDisposedException

The image has been disposed.

InvalidOperationException

An FFmpeg.Threading.FFmpegWorkQueue was not provided when the image was created, or another decoding, seeking, or automatic playback operation is currently active.

View Source

Start(bool)

Starts automatic decoding and presentation of the animation.

Declaration
public void Start(bool looping)
Parameters
Type Name Description
bool looping

true to restart the animation when its end is reached; otherwise, playback stops when the end is reached. An image with a single frame always stops after that frame.

Remarks

Automatic decoding runs independently of the calling thread.

A ReadyToRender event is raised whenever a new frame is ready to be presented. Each frame is presented for its duration (or the time to the next frame), measured from the moment it is presented. The decoding loop waits for the event handlers to complete before decoding the next frame, allowing presentation to provide back-pressure when necessary.

Automatic decoding and manual decoding or seeking are mutually exclusive. After Stop(), wait for the returned task before starting again or decoding manually.

Exceptions
Type Condition
ObjectDisposedException

The image has been disposed.

InvalidOperationException

Another decoding or seeking operation is currently active, or automatic playback is already running or still stopping.

View Source

Stop()

Stops automatic decoding and presentation.

Declaration
public Task Stop()
Returns
Type Description
Task

A task that completes when the automatic decoding operation has stopped.

Exceptions
Type Condition
ObjectDisposedException

The image has been disposed.

InvalidOperationException

A manual decoding or seeking operation is active.

Events

View Source

Faulted

Occurs when an exception terminates automatic decoding.

Declaration
public event EventHandler<Exception>? Faulted
Event Type
Type Description
EventHandler<Exception>
View Source

Finished

Occurs when automatic playback reaches the end of the animation without looping.

Declaration
public event EventHandler? Finished
Event Type
Type Description
EventHandler
Remarks

This event is not raised when playback is stopped explicitly or cancelled.

View Source

ReadyToRender

Occurs when a newly decoded frame is ready to be presented.

Declaration
public event EventHandler? ReadyToRender
Event Type
Type Description
EventHandler
Remarks

The event does not perform or require rendering. Consumers may use the event to present, copy, inspect, or otherwise process the current Frame. The event is raised on a thread-pool thread.

During automatic playback, the decoding loop waits for all event handlers to complete before continuing with the next frame. This allows a consumer to provide back-pressure when presentation takes longer than decoding.

Implements

IDisposable
IAsyncDisposable
  • View Source
In this article
Back to top Generated by DocFX