Document Solutions for Imaging
Features / Work with TIFF Images
In This Topic
    Work with TIFF Images
    In This Topic

    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.

    Reading Images from TIFF

    DsImaging provides two main classes that help extracting images from a multi-frame TIFF: GcTiffReader and TiffFrame. 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 and WicTiffFrame 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 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.
      C#
      Copy Code
      //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");
          }
      }
      
    Back to Top

    Creating a Multiframe TIFF

    To create a single-frame TIFF, you can use the GcBitmap.SaveAsTiff() 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 class with a specified file path or stream. Then, you can add various bitmaps to the output TIFF using the AppendFrame method of GcTiffWriter. Further, you can pass an instance of the TiffFrameSettings class to the GcBitmap.SaveAsTiff() method as well as to the AppendFrame() method. Also, DefaultFrameSettings 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.

    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 and Orientation properties of the TiffFrameSettings class through TiffCompression and TiffOrientation enumerations respectively.
      C#
      Copy Code
      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();
      
    Back to Top

    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 property to TiffCompressioin.Deflate or LZW. The Differencing and Planar 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 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 and GrayscaleBitmap.ShrinkPixelFormat methods). Then, you can save such an image as TIFF frame specifying the exact number of bits per channel using the BitsPer[Color]Channel or BitsPerGrayscale 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. In DsImaging, you can create tiled images by setting the TileWidth and TileHeight 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.
      C#
      Copy Code
      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();
      

    Back to Top

    For more information about working with TIFF images using DsImaging, see DsImaging sample browser.