How could I generate many mulitpage TIFF images using C# by appending them into 1 file?

I use the following code to create a multipage TIFF image.

It works fine for the creation of 1 multipage TIFF image.

Could you show me how I can modify this code so that everytime I call the MergeTwoImages function and pass it a new set of 2 more images (firstimage and secondimage), that they are appended to the outputImage?

I would like to create many multipage TIFF images into 1 file.


Byte[] img = bytes.Skip(irecpositionrevised + irectypelength).Take(intpos1revised).ToArray();

Byte[] img1 = bytes.Skip(irecpositionrevised + irectypelength + intpos1revised).Take(intpos2revised).ToArray();

System.Drawing.Bitmap newImage = MergeTwoImages(ConvertFromByteArray(img), ConvertFromByteArray(img1));


        private static Image ConvertFromByteArray(byte[] input)
            using (var ms = new MemoryStream(input))
                return Image.FromStream(ms);

        public static Bitmap MergeTwoImages(Image firstImage, Image secondImage)
            if (firstImage == null)
                throw new ArgumentNullException("firstImage");

            if (secondImage == null)
                throw new ArgumentNullException("secondImage");

            int outputImageWidth = firstImage.Width > secondImage.Width ? firstImage.Width : secondImage.Width;

            int outputImageHeight = firstImage.Height + secondImage.Height + 1;

            Bitmap outputImage = new Bitmap(outputImageWidth, outputImageHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            using (Graphics graphics = Graphics.FromImage(outputImage))
                graphics.DrawImage(firstImage, new Rectangle(new Point(), firstImage.Size),
                    new Rectangle(new Point(), firstImage.Size), GraphicsUnit.Pixel);
                graphics.DrawImage(secondImage, new Rectangle(new Point(0, firstImage.Height + 1), secondImage.Size),
                    new Rectangle(new Point(), secondImage.Size), GraphicsUnit.Pixel);

            return outputImage;
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ioannis ParaskevopoulosCommented:

This is just a sample for you to see how it should work. I will use only one image and append to the same image on the bottom 3 times, so the result will be 4 times the initial image one on the bottom of the other.:

//I am loading the same image into two objects
Image img = ConvertFromByteArray(bytes.Skip(irecpositionrevised + irectypelength).Take(intpos1revised).ToArray());
Image img1 = ConvertFromByteArray(bytes.Skip(irecpositionrevised + irectypelength).Take(intpos1revised).ToArray());
//I am merging the second image to the first and save the output to the first
//Each time this runs, the first object is increased by an img1
for(var i=1;i<3;i++)
    img = MergeTwoImages(img,img1)

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.