Image Formats
Out of the box ImageSharp supports the following image formats:
- Jpeg
- Png
- Bmp
- Gif
- Tga
ImageSharp's API however, is designed to support extension by the registration of additional IImageFormat
implementations.
Loading and Saving Specific Image Formats
Image<TPixel>
represents raw pixel data, stored in a contiguous memory block. It does not "remember" the original image format.
ImageSharp identifies image formats (Jpeg, Png, Gif etc.) by IImageFormat
instances. There are several overloads of Image.Load
capable of returning the format as an out
parameter. It's possible to pass that value to image.Save
after performing the operation:
IImageFormat format;
using (var image = Image.Load(inputStream, out format))
{
image.Mutate(c => c.Resize(30, 30));
image.Save(outputStream, format);
}
Note
ImageSharp provides common extension methods to save an image into a stream using a specific format.
image.SaveAsJpeg()
(shortcut forimage.Save(new JpegEncoder())
)image.SaveAsPng()
(shortcut forimage.Save(new PngEncoder())
)image.SaveAsGif()
(shortcut forimage.Save(new GifEncoder())
)image.SaveAsBmp()
(shortcut forimage.Save(new BmpEncoder())
)image.SaveAsTga()
(shortcut forimage.Save(new TgaEncoder())
)
A Deeper Overview of ImageSharp Format Management
Real life image streams are usually stored / transferred in standardized formats like Jpeg, Png, Bmp, Gif etc. An image format is represented by an IImageFormat
implementation.
IImageDecoder
is responsible for decoding streams (and files) in intoImage<TPixel>
. ImageSharp can auto-detect the image formats of streams/files based on their headers, selecting the correctIImageFormat
(and thusIImageDecoder
). This logic is implemented byIImageFormatDetector
's.IImageEncoder
is responsible for writingImage<TPixel>
into a stream using a given format.- Decoders/encoders and
IImageFormatDetector
's are mapped to image formats inImageFormatsManager
. It's possible to register new formats, or drop existing ones. See Configuration for more details.
Metadata-only Decoding
Sometimes it's worth to efficiently decode image metadata ignoring the memory and CPU heavy pixel information inside the stream. ImageSharp allows this by using one of the several Image.Identify overloads:
IImageInfo imageInfo = Image.Identify(inputStream);
Console.WriteLine($"{imageInfo.Width}x{imageInfo.Height} | BPP: {imageInfo.PixelType.BitsPerPixel}");
See IImageInfo
for more details about the identification result. Note that Image<TPixel>
also implements IImageInfo
.
Working with Encoders
Image formats are usually defined by complex standards allowing multiple representations for the same image. ImageSharp allows parameterizing the encoding process:
IImageEncoder
implementations are stateless, lightweight parametric objects. This means that if you want to encode a Png in a specific way (eg. changing the compression level), you need to new-up a custom PngEncoder
instance.
Choosing the right encoder parameters allows to balance between conflicting tradeoffs:
- Image file size
- Encoder speed
- Image quality
Each encoder offers options specific to the image format it represents.