ImageFrame Class
A borrowed view of one full-canvas displayed frame (or of the poster frame) of an Image.
public abstract class ImageFrameRemarks
Frames are owned by their image and are not disposable. A frame reference becomes invalid when the frame is removed, when the poster frame is replaced, or when the image is disposed; using an invalid reference throws an ObjectDisposedException. Moving frames and geometry operations keep frame identity: a reference keeps designating the same logical frame after a successful resize or reorder.
Pixel access goes through scoped leases (ImageFrame.ProcessPixelBytes, ImageFrame<TPixel>.ProcessPixelRows): the spans handed to a callback are valid only during that callback. While a lease is active, disposing the image, structural edits, storage replacement and conflicting pixel access throw an InvalidOperationException. Rows are contiguous, but the frame as a whole is not guaranteed to be stored contiguously.
Properties
| Name | Description |
|---|---|
| Height | Gets the frame height, in pixels (always the canvas height). |
| Metadata | Gets the mutable per-frame metadata, such as the display duration and the cursor hotspot. |
| PixelFormat | Gets the pixel format (always the image pixel format). |
| Size | Gets the frame size (always the canvas size). |
| Width | Gets the frame width, in pixels (always the canvas width). |
Height
ImageFrame.csMetadata
ImageFrame.csGets the mutable per-frame metadata, such as the display duration and the cursor hotspot.
public FrameMetadata Metadata { get; }PixelFormat
Gets the pixel format (always the image pixel format).
public abstract PixelFormat PixelFormat { get; }Property value
Size
ImageFrame.csGets the frame size (always the canvas size).
public Size Size { get; }Width
ImageFrame.csMethods
| Name | Description |
|---|---|
| CopyPixelBytesTo(Span<byte>, int) | Copies the visible pixel bytes to a caller buffer. Bytes between rows (stride padding) are left untouched. |
| Draw(DrawingAction, CancellationToken) | Draws on this frame only. |
| Draw<TState>(TState, DrawingAction<TState>, CancellationToken) | Draws on this frame only, passing caller state to avoid closure allocations. |
| ProcessPixelBytes(PixelBytesAction) | Gives scoped access to the visible pixel bytes, row by row, in the layout of ImageFrame.PixelFormat (16-bit components in native endianness). |
| ProcessPixelBytes<TState>(TState, PixelBytesAction<TState>) | Gives scoped access to the visible pixel bytes, passing caller state to avoid closure allocations. |
| Save(Stream, ImageEncoder) | Encodes this frame to a stream as a still image, without copying its pixels, starting at the current position of the stream. The stream is left open. |
| Save(string, ImageEncoder?) | Encodes this frame to a file as a still image, without copying its pixels. The file is written to a temporary file in the same directory and published only after encoding succeeds; on failure an existing file at path is left unchanged. |
| SaveAsync(Stream, ImageEncoder, CancellationToken) | Asynchronously encodes this frame to a stream as a still image, starting at the current position of the stream. The stream is left open. |
| SaveAsync(string, ImageEncoder?, CancellationToken) | Asynchronously encodes this frame to a file as a still image, with the output and the atomic-publication guarantees of ImageFrame.Save. |
CopyPixelBytesTo(Span<byte>, int)
ImageFrame.csCopies the visible pixel bytes to a caller buffer. Bytes between rows (stride padding) are left untouched.
Parameters
destinationSpan<byte>- The destination buffer.
strideInBytesint- The distance between the starts of two consecutive destination rows, in bytes, or 0 for tightly packed rows. Must be 0 or at least
Width * bytesPerPixel.
Exceptions
ArgumentOutOfRangeExceptionstrideInBytesis invalid.ArgumentExceptiondestinationis too short (the last row only needsWidth * bytesPerPixelbytes).InvalidOperationException- A conflicting lease is active.
ObjectDisposedException- The frame is no longer attached to a live image.
Draw overloads
Draw(DrawingAction, CancellationToken)
ImageFrame.csDraws on this frame only.
public void Draw(DrawingAction action, CancellationToken cancellationToken = default)Parameters
actionDrawingAction- The callback that draws. It must not be asynchronous and must not retain the canvas.
cancellationTokenCancellationToken- The token to monitor for cancellation requests. It is checked by every drawing call.
Exceptions
ArgumentNullExceptionactionisnull.InvalidOperationException- A conflicting lease is active.
ImageResourceLimitException- The working storage would exceed the allocation limit.
OperationCanceledException- The operation was canceled. The shapes already drawn stay, and some rows of the one being drawn may be painted.
ObjectDisposedException- The frame is no longer attached to a live image.
Remarks
The callback runs once, while the pixels of the frame are leased. See DrawingCanvas for the coordinates and the rendering rules, and Image.Draw to draw on every frame of an image.
Draw<TState>(TState, DrawingAction<TState>, CancellationToken)
ImageFrame.csDraws on this frame only, passing caller state to avoid closure allocations.
public void Draw<TState>(TState state, DrawingAction<TState> action, CancellationToken cancellationToken = default) where TState : allows ref structType parameters
TState- The type of the state.
Parameters
stateTState- The state passed to
action. actionDrawingAction<TState>- The callback that draws. Use a static lambda to avoid allocations. It must not retain the canvas.
cancellationTokenCancellationToken- The token to monitor for cancellation requests. It is checked by every drawing call.
Exceptions
ArgumentNullExceptionactionisnull.InvalidOperationException- A conflicting lease is active.
ImageResourceLimitException- The working storage would exceed the allocation limit.
OperationCanceledException- The operation was canceled. The shapes already drawn stay, and some rows of the one being drawn may be painted.
ObjectDisposedException- The frame is no longer attached to a live image.
ProcessPixelBytes overloads
ProcessPixelBytes(PixelBytesAction)
ImageFrame.csGives scoped access to the visible pixel bytes, row by row, in the layout of ImageFrame.PixelFormat (16-bit components in native endianness).
public void ProcessPixelBytes(PixelBytesAction action)Parameters
actionPixelBytesAction- The callback. It must not be asynchronous and must not retain the spans.
Exceptions
InvalidOperationException- A conflicting lease is active.
ObjectDisposedException- The frame is no longer attached to a live image.
ProcessPixelBytes<TState>(TState, PixelBytesAction<TState>)
ImageFrame.csGives scoped access to the visible pixel bytes, passing caller state to avoid closure allocations.
public void ProcessPixelBytes<TState>(TState state, PixelBytesAction<TState> action) where TState : allows ref structType parameters
TState- The type of the state.
Parameters
stateTState- The state passed to
action. actionPixelBytesAction<TState>- The callback. Use a static lambda to avoid allocations. It must not retain the spans.
Exceptions
InvalidOperationException- A conflicting lease is active.
ObjectDisposedException- The frame is no longer attached to a live image.
Save overloads
Save(Stream, ImageEncoder)
ImageFrame.csEncodes this frame to a stream as a still image, without copying its pixels, starting at the current position of the stream. The stream is left open.
public void Save(Stream stream, ImageEncoder encoder)Parameters
streamStream- The writable destination stream.
encoderImageEncoder- The encoder. Required, as a stream has no file name to infer the format from.
Exceptions
ArgumentNullExceptionstreamorencoderisnull.ArgumentExceptionstreamis not writable, or not seekable and the output needs to seek (TIFF, ANI, compressed OpenEXR).UnsupportedImageFeatureException- The frame cannot be encoded without a loss that the encoder settings do not allow.
InvalidOperationException- A pixel lease is active on this frame.
IOException- An I/O error occurred.
ObjectDisposedException- The frame is no longer attached to a live image.
Remarks
The output is the one of ImageFrame.Save.
Save(string, ImageEncoder?)
ImageFrame.csEncodes this frame to a file as a still image, without copying its pixels. The file is written to a temporary file in the same directory and published only after encoding succeeds; on failure an existing file at path is left unchanged.
public void Save(string path, ImageEncoder? encoder = null)Parameters
pathstring- The destination path.
encoderImageEncoder?- The encoder, or
nullto select it from the file extension, asImage.Savedoes.
Exceptions
ArgumentNullExceptionpathisnull.ArgumentExceptionpathis empty, orencoderisnulland the extension is not recognized.UnsupportedImageFeatureException- The frame cannot be encoded without a loss that the encoder settings do not allow (alpha, precision, metadata, canvas size).
InvalidOperationException- A pixel lease is active on this frame.
IOException- An I/O error occurred.
ObjectDisposedException- The frame is no longer attached to a live image.
Remarks
The output is the one of image.CloneFrame(index).Save(path, encoder) (or of Image.ClonePosterFrame for the poster frame): the pixels and the metadata of this frame (duration, cursor hotspot) with the metadata of its image, and no animation settings and no poster. The rules that prevent a silent loss apply to the frame as they apply to that image: for example, alpha needs JpegEncoder.BackgroundColor to be written to JPEG.
SaveAsync overloads
SaveAsync(Stream, ImageEncoder, CancellationToken)
ImageFrame.csAsynchronously encodes this frame to a stream as a still image, starting at the current position of the stream. The stream is left open.
public Task SaveAsync(Stream stream, ImageEncoder encoder, CancellationToken cancellationToken = default)Parameters
streamStream- The writable destination stream.
encoderImageEncoder- The encoder.
cancellationTokenCancellationToken- The token to monitor for cancellation requests.
Returns
Task A task that completes when the frame has been written.
Exceptions
ArgumentNullExceptionstreamorencoderisnull.ArgumentExceptionstreamis not writable, or not seekable and the output needs to seek.UnsupportedImageFeatureException- The frame cannot be encoded without a loss that the encoder settings do not allow.
InvalidOperationException- A pixel lease is active on this frame.
OperationCanceledException- The operation was canceled.
ObjectDisposedException- The frame is no longer attached to a live image.
SaveAsync(string, ImageEncoder?, CancellationToken)
ImageFrame.csAsynchronously encodes this frame to a file as a still image, with the output and the atomic-publication guarantees of ImageFrame.Save.
public Task SaveAsync(string path, ImageEncoder? encoder = null, CancellationToken cancellationToken = default)Parameters
pathstring- The destination path.
encoderImageEncoder?- The encoder, or
nullto select it from the file extension. cancellationTokenCancellationToken- The token to monitor for cancellation requests. On cancellation, no file is published.
Returns
Task A task that completes when the file has been published.
Exceptions
ArgumentNullExceptionpathisnull.ArgumentExceptionpathis empty, orencoderisnulland the extension is not recognized.UnsupportedImageFeatureException- The frame cannot be encoded without a loss that the encoder settings do not allow.
InvalidOperationException- A pixel lease is active on this frame.
OperationCanceledException- The operation was canceled.
ObjectDisposedException- The frame is no longer attached to a live image.