Table of Contents

Method Encode

Namespace
LMKit.Media.Image
Assembly
LM-Kit.NET.dll

Encode(ImageOutputFormat, ImageEncoderOptions)

Encodes the image as format into a byte array. This is THE format-driven encoder: every SaveAs* method is a fixed-option view of it.

public byte[] Encode(ImageOutputFormat format, ImageBuffer.ImageEncoderOptions options = null)

Parameters

format ImageOutputFormat

The output format.

options ImageBuffer.ImageEncoderOptions

The encoder settings; null for the defaults (a faithful re-encode, metadata carried, colour space kept).

Returns

byte[]

The encoded file bytes.

Examples

using ImageBuffer image = ImageBuffer.Load("photo.heic");
byte[] webp = image.Encode(ImageOutputFormat.Webp, new ImageBuffer.ImageEncoderOptions { Quality = 80 });
File.WriteAllBytes("photo.webp", webp);

Exceptions

ObjectDisposedException

The image has been disposed.

ArgumentException

The format cannot hold what the options ask for (CMYK in PNG or WebP, a target profile of the wrong kind).

InvalidOperationException

The encoder failed.

Encode(Stream, ImageOutputFormat, ImageEncoderOptions)

Encodes the image as format and writes the bytes to destination, which stays open.

public void Encode(Stream destination, ImageOutputFormat format, ImageBuffer.ImageEncoderOptions options = null)

Parameters

destination Stream
format ImageOutputFormat
options ImageBuffer.ImageEncoderOptions

Exceptions

ArgumentNullException

destination is null.

ArgumentException

The stream is not writable, or the format cannot hold what the options ask for.

Share