Class JpegTransform
Rotates, flips, crops and drops the color of JPEG files without decoding them to pixels, so nothing is lost: the quantized DCT
coefficients are moved and sign-flipped block by block, as jpegtran does, and coded again. Quantization tables and
chroma subsampling stay as they were. Every mode the reader takes works except lossless JPEG, which has no coefficients.
public static class JpegTransform
- Inheritance
-
JpegTransform
- Inherited Members
Examples
// Turns the photo upright by its EXIF orientation without re-encoding: no quality is lost, and the tag is reset.
JpegTransform.Apply("sideways.jpg", "upright.jpg", new JpegTransformOptions { Operation = JpegTransformOperation.AutoOrient });
// Rotates, crops and drops the color in one pass; a file can be transformed in place.
JpegTransformResult result = JpegTransform.Apply("upright.jpg", "upright.jpg", new JpegTransformOptions
{
Operation = JpegTransformOperation.Rotate90,
Crop = new Rect(0, 0, 200, 120),
Grayscale = true,
});
Console.WriteLine($"{result.Width} × {result.Height}");
Remarks
The whole image's coefficients are held while it runs (about 2 bytes per sample, 3 bytes per pixel for a 4:2:0 photo), counted against MaxTotalBytes. The result is the same for any MaxParallelism.
Methods
Apply(Stream, Stream, JpegTransformOptions, EgamiContext?)
Transforms the JPEG in source and writes the result to destination.
public static JpegTransformResult Apply(Stream source, Stream destination, JpegTransformOptions options, EgamiContext? context = null)
Parameters
sourceStreamThe JPEG, read from its current position. It is not disposed.
destinationStreamWhere the result goes. It is not disposed.
optionsJpegTransformOptionsWhat to do.
contextEgamiContextThe context whose limits and diagnostics apply, or null for Default.
Returns
- JpegTransformResult
The size of the result and the warnings.
Examples
// Turns the photo upright by its EXIF orientation without re-encoding: no quality is lost, and the tag is reset.
JpegTransform.Apply("sideways.jpg", "upright.jpg", new JpegTransformOptions { Operation = JpegTransformOperation.AutoOrient });
// Rotates, crops and drops the color in one pass; a file can be transformed in place.
JpegTransformResult result = JpegTransform.Apply("upright.jpg", "upright.jpg", new JpegTransformOptions
{
Operation = JpegTransformOperation.Rotate90,
Crop = new Rect(0, 0, 200, 120),
Grayscale = true,
});
Console.WriteLine($"{result.Width} × {result.Height}");
Exceptions
- ArgumentNullException
An argument is null.
- ArgumentOutOfRangeException
Crop is empty or leaves the image.
- InvalidOperationException
Strict and a partial MCU would move.
- NotSupportedException
Grayscale on an RGB, CMYK or YCCK file.
- UnsupportedFormatException
The file is lossless or hierarchical JPEG.
- CorruptDataException
The file breaks the format's rules beyond repair.
- LimitExceededException
The image is larger than the limits allow, or its coefficients need more memory.
Apply(string, string, JpegTransformOptions, EgamiContext?)
Transforms a JPEG file into another, or into itself: the result goes to a temporary file beside
destinationPath that replaces it once complete, so a failure leaves the destination as it was.
public static JpegTransformResult Apply(string sourcePath, string destinationPath, JpegTransformOptions options, EgamiContext? context = null)
Parameters
sourcePathstringThe JPEG file.
destinationPathstringThe file to write; it may be
sourcePath.optionsJpegTransformOptionsWhat to do.
contextEgamiContextThe context whose limits and diagnostics apply, or null for Default.
Returns
- JpegTransformResult
The size of the result and the warnings.
Examples
// Turns the photo upright by its EXIF orientation without re-encoding: no quality is lost, and the tag is reset.
JpegTransform.Apply("sideways.jpg", "upright.jpg", new JpegTransformOptions { Operation = JpegTransformOperation.AutoOrient });
// Rotates, crops and drops the color in one pass; a file can be transformed in place.
JpegTransformResult result = JpegTransform.Apply("upright.jpg", "upright.jpg", new JpegTransformOptions
{
Operation = JpegTransformOperation.Rotate90,
Crop = new Rect(0, 0, 200, 120),
Grayscale = true,
});
Console.WriteLine($"{result.Width} × {result.Height}");
Exceptions
- ArgumentNullException
An argument is null.
- ArgumentOutOfRangeException
Crop is empty or leaves the image.
- InvalidOperationException
Strict and a partial MCU would move.
- NotSupportedException
Grayscale on an RGB, CMYK or YCCK file.
- UnsupportedFormatException
The file is lossless or hierarchical JPEG.
- CorruptDataException
The file breaks the format's rules beyond repair.
- LimitExceededException
The image is larger than the limits allow, or its coefficients need more memory.
- IOException
A file cannot be read or written.