# Work with TIFF Images

TIFF is a file format used for storing raster images. With DsImaging, extract images from a multi-frame TIFF, create a multi-frame TIFF or a tiled image.

## Content

Tagged Image File Format (TIFF) is a widely used file format for storing raster images. A primary goal of TIFF is to provide a rich environment within which applications can exchange image data. TIFF can describe bi-level, grayscale, palette-color, and full-color images with optional transparency and Exif metadata. It supports several compression schemes that allow developers to choose the best space or time tradeoff for their applications. In general, TIFF can store lossless and lossy (JPEG-based) image data. DsImaging supports only lossless compression for TIFF frames. PNG format usually offers slightly better compression ratio, but it is limited to one image per file. TIFF can store multiple images in the same file. For more info see the [Adobe TIFF specifications](https://web.archive.org/web/20210108174645/https://www.adobe.io/content/dam/udp/en/open/standards/tiff/TIFF6.pdf).

## Reading Images from TIFF

DsImaging provides two main classes that help extracting images from a multi-frame TIFF: [GcTiffReader](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GcTiffReader.html) and [TiffFrame](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.TiffFrame.html). To read an image from a single-frame TIFF, just load the image into a GcBitmap as other supported image formats, like JPEG or BMP. Also, when a TIFF file contains JPEG-based frames, you can use the platform-dependent [GcWicTiffReader](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging.Windows/GrapeCity.Documents.Imaging.Windows.GcWicTiffReader.html) and [WicTiffFrame](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging.Windows/GrapeCity.Documents.Imaging.Windows.WicTiffFrame.html) classes from GrapeCity.Documents.Imaging.Windows namespace. However, there is no such option available for non-Windows systems.
**GcTiffReader** accepts a file name or stream as the constructor argument and immediately loads the contents of TIFF without loading the actual image data. The information about TIFF frames is collected in the [Frames](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GcTiffReader.Frames.html) property of the GcTiffReader class. The list contains objects of type **TiffFrame** providing the detailed information about the specific frame, including its size, format, and various metadata. Also, TiffFrame allows to read the frame image into the regular image storing classes of DsImaging, such as GcBitmap, BilevelBitmap, GrayscaleBitmap, and palette-based bitmaps. These images can be processed in a number of ways, stored in different formats or added as frames to a GcTiffWriter.
To read a multiframe TIFF and save its frames as separate images:

1. Initialize the **GcTiffReader** class and pass the multi frame TIFF as a parameter to the constructor.
2. Access the list of frames from the TIFF image using **Frames** property of the GcTiffReader class.
3. Invoke the **ReadAsGcBitmap** method to get the frame image as GcBitmap object.
4. Save the image to a file in PNG format using **SaveAsPng** method.

    ```csharp
    //Initialize TiffReader class and load the Tiff image
    string tiffFilePath = Path.Combine("Resources", "Images", "Test.tif");
    GcTiffReader tr = new GcTiffReader(tiffFilePath);
    
    string pngName = "FrameImage";
    
    //Save separate images for each Tiff frame
    for (int i = 0; i < tr.Frames.Count; i++)
    {
        using (var bmp = tr.Frames[i].ReadAsGcBitmap())
        {
            bmp.SaveAsPng($"{pngName}_{(i + 1)}.png");
        }
    }
    ```

## Creating a Multiframe TIFF

To create a single-frame TIFF, you can use the [GcBitmap.SaveAsTiff()](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GcBitmap.SaveAsTiff.html) method which accepts either file path or the output stream as an argument. Now, you can create a multi-frame TIFF by creating an instance of the [GcTiffWriter](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GcTiffWriter.html) class with a specified file path or stream. Then, you can add various bitmaps to the output TIFF using the [AppendFrame](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GcTiffWriter.AppendFrame.html) method of GcTiffWriter. Further, you can pass an instance of the [TiffFrameSettings](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.TiffFrameSettings.html) class to the GcBitmap.SaveAsTiff() method as well as to the AppendFrame() method. Also, [DefaultFrameSettings](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GcTiffWriter.DefaultFrameSettings.html) property of the GcTiffWriter class allows you to create the common settings for all the frames. For more information on TIFF frame settings, see [TIFF Configuration Options](/document-solutions/dot-net-imaging-api/docs/online/features/workwithtiffimages#tiff-configuration-options).
To create a multiframe TIFF by combining four images:

1. Create an instance of the GcBitmap class to load the images which will serve as frames for the multiframe TIFF.
2. Initialize the **GcTiffWriter** class by passing the output file name as its parameter.
3. Invoke the **AppendFrame** method of GcTiffWriter class for each frame to write frames to the output stream.
4. Optionally, set the compression and orientation of the frame using [Compression](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.TiffFrameSettings.Compression.html) and [Orientation](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.TiffFrameSettings.Orientation.html) properties of the TiffFrameSettings class through [TiffCompression](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.TiffCompression.html) and [TiffOrientation](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.TiffOrientation.html) enumerations respectively.

    ```csharp
    string imagePath = Path.Combine("Resources", "Images", "MultiFrameTiff.tif");
    
    //Initialize TiffWriter class to generate multi-frame TIFF
    GcTiffWriter tiffWriter = new GcTiffWriter(imagePath);
    
    //Define Tiff frame settings
    TiffFrameSettings settings = new TiffFrameSettings();
    settings.Compression = TiffCompression.PackBits;
    settings.Orientation = TiffOrientation.TopLeft;
    
    //Initialize GcBitmap to load images for frames
    GcBitmap origbmp = new GcBitmap();
    
    //Load image and append first frame
    imagePath = Path.Combine("Resources", "Images", "TiffFrames", "Img1.png");
    settings.ImageDescription = "Frame1";
    origbmp.Load(imagePath);
    tiffWriter.AppendFrame(origbmp, settings);
    
    //Load image and append second frame
    imagePath = Path.Combine("Resources", "Images", "TiffFrames", "Img2.png");
    origbmp.Load(imagePath);
    settings.ImageDescription = "Frame2";
    tiffWriter.AppendFrame(origbmp, settings);
    
    tiffWriter.Dispose();
    ```

## TIFF Configuration Options

DsImaging gives full control over the format and settings of an output TIFF frame with the **TiffFrameSettings** class. The frame settings include various metadata, such as the image description, the date of image creation and so on. Also, there are some important properties controlling the compression scheme of the frame image. For the best compression of a full-color image, you can set the [Compression](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.TiffFrameSettings.Compression.html) property to TiffCompressioin.**Deflate** or **LZW**. The [Differencing](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.TiffFrameSettings.Differencing.html) and [Planar](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.TiffFrameSettings.Planar.html) properties also can help in better compression results. In the case of bilevel and grayscale images, the other compression schemes can also fit well. With [GcBitmap](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GcBitmap.html) it is possible to shrink the color channels (Red, Green, Blue, Alpha) from 8 bits to some lower value using one of the error-diffusion algorithms (see GcBitmap.[ShrinkARGBFormat](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GcBitmap.ShrinkARGBFormat.html) and GrayscaleBitmap.[ShrinkPixelFormat](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.GrayscaleBitmap.ShrinkPixelFormat.html) methods). Then, you can save such an image as TIFF frame specifying the exact number of bits per channel using the [BitsPer\[Color\]Channel](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.TiffFrameSettings.BitsPerBlueChannel.html) or [BitsPerGrayscale](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.TiffFrameSettings.BitsPerGrayscale.html) properties of TiffFrameSettings. Before doing that please make sure that, just like **GcTiffReader**, your TIFF viewer application supports TIFF frames with variable bits per channel.

## Creating Tiled image

Tiled TIFF frames are, generally preferred over stripped frames in case of large images as well as for images where the color areas change more frequently in the horizontal direction than in vertical. For more information, see [“Tiled Images” section in the TIFF specification](https://web.archive.org/web/20210108174645/https://www.adobe.io/content/dam/udp/en/open/standards/tiff/TIFF6.pdf). In DsImaging, you can create tiled images by setting the [TileWidth](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.TiffFrameSettings.TileWidth.html) and [TileHeight](/document-solutions/dot-net-imaging-api/api/online/DS.Documents.Imaging/GrapeCity.Documents.Imaging.TiffFrameSettings.TileHeight.html) properties to some positive values. Please note that it might affect the compression ratio.
To create a tiled TIFF image consisting of four frames:

1. Create an instance of the **GcBitmap** class to load the images which will serve as frames for the multiframe TIFF.
2. Initialize the **GcTiffWriter** class by passing the output file name as its parameter.
3. Also, set the tile height and tile width using the **TileHeight** and **TileWidth** properties of the TiffFrameSettings class.
4. Invoke the **AppendFrame** method of GcTiffWriter class for each frame to write frames to the output stream.

    ```csharp
    string imagePath = Path.Combine("Resources", "Images", "TiledTiff.tif");
    
    //Initialize TiffWriter class to generate multi-frame TIFF
    GcTiffWriter tiffWriter = new GcTiffWriter(imagePath);
    
    //Define Tiff frame settings
    TiffFrameSettings settings = new TiffFrameSettings();
    settings.TileHeight = 200;
    settings.TileWidth = 200;
    
    //Initialize GcBitmap to load images for frames
    GcBitmap origbmp = new GcBitmap();
    
    //Load image and append first frame
    imagePath = Path.Combine("Resources", "Images", "TiffFrames", "Img1.png");
    settings.ImageDescription = "Frame1";
    origbmp.Load(imagePath);
    tiffWriter.AppendFrame(origbmp, settings);
    
    //Load image and append second frame
    imagePath = Path.Combine("Resources", "Images", "TiffFrames", "Img2.png");
    origbmp.Load(imagePath);
    settings.ImageDescription = "Frame2";
    tiffWriter.AppendFrame(origbmp, settings);
    
    tiffWriter.Dispose();
    ```

For more information about working with TIFF images using DsImaging, see [DsImaging sample browser](https://developer.mescius.com/documents-api-imaging/demos/basics/tiff/extract-frames/code-cs).