• Articles
  • API Documentation
Search Results for

    Show / Hide Table of Contents
    • ImageSharp
      • Getting Started
        • Pixel Formats
        • Image Formats
        • Processing Images
          • Resizing Images
        • Working with Pixel Buffers
        • Configuration
        • Memory Management
    • 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:

    • 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 for image.Save(new JpegEncoder()))
    • image.SaveAsPng() (shortcut for image.Save(new PngEncoder()))
    • image.SaveAsGif() (shortcut for image.Save(new GifEncoder()))
    • image.SaveAsBmp() (shortcut for image.Save(new BmpEncoder()))
    • image.SaveAsTga() (shortcut for image.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 into Image<TPixel>. ImageSharp can auto-detect the image formats of streams/files based on their headers, selecting the correct IImageFormat (and thus IImageDecoder). This logic is implemented by IImageFormatDetector's.
    • IImageEncoder 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.

    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.

    • Improve this Doc
    In This Article
    Back to top Generated by DocFX