Method Encode
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
formatImageOutputFormatThe output format.
optionsImageBuffer.ImageEncoderOptionsThe encoder settings;
nullfor 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
destinationStreamformatImageOutputFormatoptionsImageBuffer.ImageEncoderOptions
Exceptions
- ArgumentNullException
destinationis null.- ArgumentException
The stream is not writable, or the format cannot hold what the options ask for.