.net – Merging multiple TIFF files in one TIFF file


I've been trying to use the mergeTiffStreams method of the TiffUtil class posted at:


However, like other people have commented on that thread, the end result is that I receive a Stream that only contains my first TIFF file. The other TIFFs that I passed in the MemoryStream array are not included.

Has anyone else run into this problem before? If so, any advice would be most appreciated.



Here's the code I have at the moment. The stream that it returns only contains the first TIFF from the array passed in.

    public MemoryStream mergeTiffStreams(MemoryStream[] tiffStreams)
        if (tiffStreams == null)
            throw new ArgumentNullException("tiffStreams");

        Encoder enc = Encoder.SaveFlag;

        ImageCodecInfo info = null;
        foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
            if (ice.MimeType == "image/tiff")
                info = ice;

        EncoderParameters ep = new EncoderParameters(1);
        ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
        MemoryStream tiffStream = new MemoryStream();
        Bitmap masterBitmap = new Bitmap(tiffStreams[0]);
        masterBitmap.Save(tiffStream, info, ep);

        ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
        for (int x = 1; x < tiffStreams.Length; x++)
            masterBitmap.SaveAdd(Image.FromStream(tiffStreams[x]), ep);

        ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);

        return tiffStream;

Best Solution

Your problem might be in how you're handling the stream returned. If, for example, you want to save it to IO use:

File.WriteAllBytes(myPath, tiffStream.ToArray());

rather than creating a new Bitmap object from the stream. That was my problem anyway.

Related Question