• Articles
  • API Documentation
Search Results for

    Show / Hide Table of Contents
    • ImageSharp
      • Getting Started
        • Pixel Formats
        • Image Formats
        • Processing Images
          • Resizing Images
          • Create an animated GIF
        • Working with Pixel Buffers
        • Configuration
        • Memory Management
        • Security Considerations
    • ImageSharp.Drawing
      • Getting Started
    • ImageSharp.Web
      • Getting Started
        • Processing Commands
        • Image Providers
        • Image Caches
    • Fonts
      • Getting Started
      • Custom Rendering

    Image Formats

    Out of the box ImageSharp supports the following image formats:

    • Bmp
    • Gif
    • Jpeg
    • Pbm
    • Png
    • Tiff
    • Tga
    • WebP
    • Qoi

    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. Decoded images store the format in the DecodedImageFormat within the image metadata. It is possible to pass that value to image.Save after performing the operation:

    using (var image = Image.Load(inputStream))
    {
        image.Mutate(c => c.Resize(30, 30));
        image.Save(outputStream, image.Metadata.DecodedImageFormat);
    }
    
    Note

    ImageSharp provides common extension methods to save an image into a stream using a specific format.

    • image.SaveAsBmp() (shortcut for image.Save(new BmpEncoder()))
    • image.SaveAsGif() (shortcut for image.Save(new GifEncoder()))
    • image.SaveAsJpeg() (shortcut for image.Save(new JpegEncoder()))
    • image.SaveAsPbm() (shortcut for image.Save(new PbmEncoder()))
    • image.SaveAsPng() (shortcut for image.Save(new PngEncoder()))
    • image.SaveAsTga() (shortcut for image.Save(new TgaEncoder()))
    • image.SaveAsTiff() (shortcut for image.Save(new TiffEncoder()))
    • image.SaveAsWebp() (shortcut for image.Save(new WebpEncoder()))
    • image.SaveAsQoi() (shortcut for image.Save(new QoiEncoder()))

    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.

    • ImageDecoder is responsible for decoding streams (and files) in into Image<TPixel>. ImageSharp can auto-detect the image formats of streams/files based on their headers, selecting the correct IImageFormat (and thus ImageDecoder). This logic is implemented by IImageFormatDetector's.
    • ImageEncoder is responsible for writing Image<TPixel> into a stream using a given format.
    • Decoders/encoders and IImageFormatDetector's are mapped to image formats in ImageFormatsManager. It's possible to register new formats, or drop existing ones. See Configuration for more details.

    Working with Decoders

    The behavior of the various decoders during the decoding process can be controlled by passing DecoderOptions instances to our general Load APIs. These options contain means to control metadata handling, the decoded frame count, and properties to allow directly decoding the encoded image to a given target size.

    Specialized Decoding

    In addition to the general decoding API we offer additional specialized decoding options ISpecializedDecoderOptions that can be accessed directly against ISpecializedDecoder<T> instances which provide further options for decoding.

    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:

    ImageInfo imageInfo = Image.Identify(inputStream);
    Console.WriteLine($"{imageInfo.Width}x{imageInfo.Height} | BPP: {imageInfo.PixelType.BitsPerPixel}");
    

    See ImageInfo for more details about the identification result.

    Working with Encoders

    Image formats are usually defined by complex standards allowing multiple representations for the same image. ImageSharp allows parameterizing the encoding process: ImageEncoder 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.

    • Edit this page
    In this article
    Back to top Generated by DocFX