TiffEncoder Class

Immutable settings for TIFF and BigTIFF encoding.

public sealed class TiffEncoder : ImageEncoder

Remarks

The container is the part of TIFF 6.0 that any reader accepts: little-endian by default, chunky samples, top-down strips, no predictor for integer samples. TiffEncoder.Compression selects no compression, Deflate (the default), LZW, PackBits, the CCITT facsimile compressions or JPEG. TiffEncoder.Predictor selects horizontal differencing for the integer samples of Deflate and LZW pages, and TiffEncoder.TileWidth and TiffEncoder.TileHeight write tiles instead of strips. Planar storage is never written.

By default (TiffPixelLayout.Auto) the sample layout follows the pixel format of the image, losslessly: Gray8/Gray16 become 8- or 16-bit BlackIsZero grayscale, La16/La32 (and A8, written as black with its alpha) the same grayscale with an alpha sample, Rgb24/Rgb48 8- or 16-bit RGB, and the other formats with alpha RGBA, the alpha with ExtraSamples = 2 (unassociated, straight alpha). Nothing is quantized and alpha is never dropped. RgbaFloat and RgbaHalf become 32-bit floating-point samples (SampleFormat = 3), every value kept, in the fewest samples that hold the page: gray when the three colors of every pixel are the same number and no ICC profile is written, and without alpha when every alpha is exactly 1. Deflate and LZW strips of floating-point samples use the floating-point predictor of TIFF Technical Note 3 (Predictor = 3). A floating-point page without ICC profile is read as linear light, so its pixels must be labeled ColorTransferFunction.Linear (the label of a new floating-point image) or described by a profile; otherwise ImageEncoder.MetadataHandling decides. The bilevel and palette layouts and JPEG compression do not store floating-point samples: ImageEncoder.FloatingPointConversion reduces them first. TiffEncoder.PixelLayout also selects a bilevel layout (one bit per pixel) and a palette layout, which store the image exactly or reject it: this encoder has no background color, no bit-depth-reduction switch and no color reduction. The only output that is not exact is the one of TiffCompression.Jpeg, which is lossy by nature and selected explicitly. CMYK data is never written.

The destination must be seekable. A TIFF directory stores the offsets of the strips it describes, so it can only be written once their compressed sizes are known; the encoder writes the pixel data first and then patches the header and the directory pointers. A non-seekable stream is rejected before anything is written. File destinations are always seekable.

One Image.Save writes a one-page document; use ImageCollection.Save to write several pages, whose sizes, pixel formats and metadata may all differ. Animated images are rejected: the pages of a document are not animation frames.

Metadata (subject to ImageEncoder.MetadataHandling): the resolution (XResolution/YResolution/ResolutionUnit), the orientation (Orientation), the ICC profile (InterColorProfile) and the XMP packet (XMP). An EXIF block and text entries are not stored (rejected by default). A profile describes the written samples: a bilevel page is grayscale and a palette page is RGB, whatever the pixel format of the image, so a profile of the other color space is metadata the page cannot store.

Inheritance
ImageEncoder TiffEncoder

Constructors

NameDescription
TiffEncoder()

TiffEncoder()

TiffEncoder.cs
public TiffEncoder()

No documentation is available.

Properties

NameDescription
BigEndianGets a value indicating whether to write big-endian (MM) instead of little-endian (II) files. Defaults to false. Both byte orders are equally valid TIFF; this only changes the bytes, never the pixels.
BigTiffGets a value indicating whether to write a BigTIFF (magic 43, 64-bit offsets) instead of a classic TIFF. Defaults to false: a classic TIFF is accepted by far more readers, and nothing this encoder writes needs 64-bit offsets below 4 GiB.
CompressionGets the compression of the strips. Defaults to TiffCompression.Deflate.
FormatGets the format produced by this encoder.
JpegChromaSubsamplingGets the chroma subsampling of TiffCompression.Jpeg for the color pixel formats, with the meaning of JpegEncoder.ChromaSubsampling. Defaults to JpegChromaSubsampling.Auto (4:2:0). Ignored by the other compressions and by the gray pixel formats.
JpegQualityGets the quality of TiffCompression.Jpeg, from 1 (smallest output) to 100 (best quality), with the meaning of JpegEncoder.Quality. Defaults to 90. Ignored by the other compressions.
PixelLayoutGets the sample layout. Defaults to TiffPixelLayout.Auto.
PredictorGets the predictor of the integer samples. Defaults to TiffPredictor.None, which writes the same file as before the setting existed.
RowsPerStripGets the number of rows of one strip, or 0 (the default) to choose a number of rows whose uncompressed size is close to TiffEncoder.DefaultStripBytes, so that encoding one page needs a bounded amount of memory whatever the size of the page. Ignored by TiffCompression.Jpeg, which writes each page as one strip, and by tiled pages (TiffEncoder.TileWidth).
TileHeightGets the height of the tiles (TileLength), in pixels: a positive multiple of 16, or 0 (the default) to write strips. Set together with TiffEncoder.TileWidth.
TileWidthGets the width of the tiles, in pixels: a positive multiple of 16, or 0 (the default) to write strips. Set together with TiffEncoder.TileHeight.

Gets a value indicating whether to write big-endian (MM) instead of little-endian (II) files. Defaults to false. Both byte orders are equally valid TIFF; this only changes the bytes, never the pixels.

public bool BigEndian { get; init; }

Property value

bool

Gets a value indicating whether to write a BigTIFF (magic 43, 64-bit offsets) instead of a classic TIFF. Defaults to false: a classic TIFF is accepted by far more readers, and nothing this encoder writes needs 64-bit offsets below 4 GiB.

public bool BigTiff { get; init; }

Property value

bool

Compression

TiffEncoder.cs

Gets the compression of the strips. Defaults to TiffCompression.Deflate.

public TiffCompression Compression { get; init; }

Property value

TiffCompression

Exceptions

ArgumentOutOfRangeException
The value is not a defined TiffCompression.

Gets the format produced by this encoder.

public override ImageFormat Format { get; }

Property value

ImageFormat

JpegChromaSubsampling

TiffEncoder.cs

Gets the chroma subsampling of TiffCompression.Jpeg for the color pixel formats, with the meaning of JpegEncoder.ChromaSubsampling. Defaults to JpegChromaSubsampling.Auto (4:2:0). Ignored by the other compressions and by the gray pixel formats.

public JpegChromaSubsampling JpegChromaSubsampling { get; init; }

JpegQuality

TiffEncoder.cs

Gets the quality of TiffCompression.Jpeg, from 1 (smallest output) to 100 (best quality), with the meaning of JpegEncoder.Quality. Defaults to 90. Ignored by the other compressions.

public int JpegQuality { get; init; }

Property value

int

Exceptions

ArgumentOutOfRangeException
The value is not between 1 and 100.

PixelLayout

TiffEncoder.cs

Gets the sample layout. Defaults to TiffPixelLayout.Auto.

public TiffPixelLayout PixelLayout { get; init; }

Gets the predictor of the integer samples. Defaults to TiffPredictor.None, which writes the same file as before the setting existed.

public TiffPredictor Predictor { get; init; }

Property value

TiffPredictor

Exceptions

ArgumentOutOfRangeException
The value is not a defined TiffPredictor.

Remarks

TiffPredictor.HorizontalDifferencing requires TiffCompression.Deflate or TiffCompression.Lzw (the compressions readers apply a predictor with) and TiffPixelLayout.Auto (bilevel samples and palette indexes are not predictable); saving fails with an ArgumentException otherwise. It applies to the 8- and 16-bit samples of every pixel format; the floating-point samples keep the floating-point predictor.

RowsPerStrip

TiffEncoder.cs

Gets the number of rows of one strip, or 0 (the default) to choose a number of rows whose uncompressed size is close to TiffEncoder.DefaultStripBytes, so that encoding one page needs a bounded amount of memory whatever the size of the page. Ignored by TiffCompression.Jpeg, which writes each page as one strip, and by tiled pages (TiffEncoder.TileWidth).

public int RowsPerStrip { get; init; }

Property value

int

Exceptions

ArgumentOutOfRangeException
The value is negative.

Remarks

With ImageConfiguration.MaxDegreeOfParallelism above 1 (the configuration of the saved image), the LZW and Deflate strips (or tiles) of a page of at least 65,536 pixels are compressed by up to that many workers, one strip each at a time: the file is byte-identical to the one of one worker, and each worker holds a strip and its compressed bytes, charged to ImageResourceLimits.MaxLiveAllocationBytes. A page stored as one strip is compressed by one worker.

TileHeight

TiffEncoder.cs

Gets the height of the tiles (TileLength), in pixels: a positive multiple of 16, or 0 (the default) to write strips. Set together with TiffEncoder.TileWidth.

public int TileHeight { get; init; }

Property value

int

Exceptions

ArgumentOutOfRangeException
The value is negative or not a multiple of 16.

Gets the width of the tiles, in pixels: a positive multiple of 16, or 0 (the default) to write strips. Set together with TiffEncoder.TileHeight.

public int TileWidth { get; init; }

Property value

int

Exceptions

ArgumentOutOfRangeException
The value is negative or not a multiple of 16.

Remarks

A tiled page (TileWidth, TileLength, TileOffsets and TileByteCounts) is cut into tiles of the same size, left to right and top to bottom; the tiles on the right and bottom edges extend past the page and are padded with zero samples, as TIFF requires. Readers can then decode a region of a large page without reading the rest. TiffEncoder.RowsPerStrip is ignored. Saving fails with an ArgumentException when only one of the two sizes is set, or with TiffCompression.Jpeg, which writes each page as one strip.

Constants

NameDescription
DefaultStripBytesThe default number of uncompressed bytes the encoder aims for in one strip (64 KiB).

DefaultStripBytes

The default number of uncompressed bytes the encoder aims for in one strip (64 KiB).

public const int DefaultStripBytes = 65536