Search code examples
c#.netimage-processingdrawingtiff

Merge multiple multi-page tiff images to a single tiff C#


In my scenario I have 3 or more multi-page tiff images which I need to merge into a single tiff image.

Below is the the code I have tried. It merges in to a single tiff image but only with first page of all tiff images.

private static void MergeTiff(string[] sourceFiles)
{
    string[] sa = sourceFiles;
    //get the codec for tiff files
    ImageCodecInfo info = null;
    foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
        if (ice.MimeType == "image/tiff")
            info = ice;

    //use the save encoder
    Encoder enc = Encoder.SaveFlag;

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

    Bitmap pages = null;

    int frame = 0;

    foreach (string s in sa)
    {
        if (frame == 0)
        {
            MemoryStream ms = new MemoryStream(File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, @"C:\Data_Warehouse\SVNRepository\CD.BNS.W5555.LT45555C.D180306.T113850.Z0101\", s)));
            pages = (Bitmap)Image.FromStream(ms);

            var appDataPath = @"C:\Data_Warehouse\SVNRepository\Tiffiles\";
            var filePath = Path.Combine(appDataPath, Path.GetRandomFileName() + ".tif");

            //save the first frame
            pages.Save(filePath, info, ep);
        }
        else
        {
            //save the intermediate frames
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

            try
            {
                MemoryStream mss = new MemoryStream(File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, @"C:\Data_Warehouse\SVNRepository\CD.BNS.W5555.LT45555C.D180306.T113850.Z0101\", s)));
                Bitmap bm = (Bitmap)Image.FromStream(mss);
                pages.SaveAdd(bm, ep);
            }
            catch (Exception e)
            {
                //LogError(e, s);
            }
        }

        if (frame == sa.Length - 1)
        {
            //flush and close.
            ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
            pages.SaveAdd(ep);

        }

        frame++;
    }

}

I need to join multiple tiff images with all pages from each tiff image. Please advise!

Thanks

EDIT: Updated from below answer

if (frame == 0)
            {
                MemoryStream ms = new MemoryStream(File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, @"C:\OMTest\Working\", s)));
                pages = (Bitmap)Image.FromStream(ms);

                var appDataPath = @"C:\Data_Warehouse\SVNRepository\Tiffiles\";
                var filePath = Path.Combine(appDataPath, Path.GetRandomFileName() + ".tif");

                //save the first frame
                pages.Save(filePath, info, ep);

                //Save the second frame if any
                int frameCount1 = pages.GetFrameCount(FrameDimension.Page);
                if (frameCount1 > 1)
                {
                    for (int i = 1; i < frameCount1; i++)
                    {
                        ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
                        pages.SelectActiveFrame(FrameDimension.Page, i);
                        pages.SaveAdd(pages, ep);
                    }
                }
            }
            else
            {
                //save the intermediate frames
                ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
                try
                {
                    MemoryStream mss = new MemoryStream(File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, @"C:\OMTest\Working\", s)));
                    Bitmap bm = (Bitmap)Image.FromStream(mss);
                    int frameCount = bm.GetFrameCount(FrameDimension.Page);
                    for (int i = 0; i < frameCount; i++)
                    {
                        bm.SelectActiveFrame(FrameDimension.Page, i);
                        pages.SaveAdd(bm, ep);
                    }
                }
                catch (Exception e)
                {
                    //LogError(e, s);
                }
            }

Solution

  • You need to select the active frame to ensure you are getting all pages on the TIFF. In your code you need to get the count of frames and loop through these.

    The code in your else block might look something like this:

    MemoryStream mss = new MemoryStream(File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, @"C:\Data_Warehouse\SVNRepository\CD.BNS.W5555.LT45555C.D180306.T113850.Z0101\", s)));
    Bitmap bm = (Bitmap)Image.FromStream(mss);
    int frameCount = bm.GetFrameCount(FrameDimension.Page);
    for(int i=0;i<frameCount;i++){
        bm.SelectActiveFrame(FrameDimension.Page, i);
        pages.SaveAdd(bm, ep);
    }
    

    You may have to tweak it as I haven't tested it.