I am wring a console application in C# using VS2010 with Framework 4.0 and do not know how to write multiple TIFF images to a file?

I am writing a C# console application using VS2010 with Framework Version 4.0 to generate multipage TIFF images and save these images to a file.

  The variable "img" contains the front image of a check.

  The variable "img2" contains the back image of a check.

  Function A is performed for each pair of front image (img) and back images (img2) of each check processed from an input file:

  Funtion A
  {

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

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

     newImage[CheckOrderNum - 1] = MergeTwoImages(ConvertFromByteArray(img), ConvertFromByteArray(img1));
  }


  After all checks have been read & processed via Function A, the following foreach loop is performed to write out
  ALL multipage TIFF images:

  foreach (Image image in newImage)
  {
      image.Save("L:\\F\\Source\\test1.tiff", System.Drawing.Imaging.ImageFormat.Tiff);   <-- this Save only Saves the current TIFF Image            
 }  

My goal is to write out ALL mulitpage TIFF Images to a file titled test1.tiff, so that I can see the front image & back image of ALL checks my application reads from an input file.

 Currenlty my program OVERWRITES the existing TIFF multipage Image with the most recent TIFF mulitpage each time the file is saved.


-----------------------------------------------------------------

I believe the following approach will work for Version 4.5 but unfortunately I am working with Version 4.0 (VS2010)

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;

namespace EE_Q28711383
{
      class Program
      {
            static void Main(string[] args)
            {
                  List<Image> images = new List<Image>();
                  images.Add(Image.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "image1.jpg")));
                  images.Add(Image.FromFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "image2.jpg")));
                  images.MergeImages(new FileInfo(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "image3.tiff")));
            }
      }

      static class Extensions
      {
            public static void MergeImages(this IEnumerable<Image> images, string fileName)
            {
                  images.MergeImages(new FileInfo(fileName));
            }

            public static void MergeImages(this IEnumerable<Image> images, FileInfo file)
            {
                  if (images == null)
                        throw new ArgumentNullException("images", "Images cannot be null");

                  if (file == null)
                        throw new ArgumentNullException("file", "File cannot be null");

                  ImageCodecInfo codec = null;
                  Image tiff = null;
                  MemoryStream stream = null;
                  EncoderParameters parameters = null;
                  try
                  {
                        codec = (from _codec in ImageCodecInfo.GetImageEncoders() where _codec.MimeType.Equals("image/tiff", StringComparison.OrdinalIgnoreCase) select _codec).FirstOrDefault();
                        if (codec != default(ImageCodecInfo))
                        {
                              bool first = true;
                              parameters = new EncoderParameters(2);
                              parameters.Param[0] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionCCITT4);
                              parameters.Param[1] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.MultiFrame);

                              foreach (Image image in images)
                              {
                                    stream = new MemoryStream();
                                    image.Save(stream, ImageFormat.Tiff);

                                    if (first)
                                    {
                                          tiff = Image.FromStream(stream);
                                          tiff.Save(file.FullName, codec, parameters);
                                          first = false;
                                    }
                                    else
                                    {
                                          parameters.Param[1] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
                                          tiff.SaveAdd(Image.FromStream(stream), parameters);
                                    }
                                    stream.Dispose();
                                    image.Dispose();
                              }

                              parameters.Param[1] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.Flush);
                              tiff.SaveAdd(parameters);
                        }
                  }
                  catch (Exception ex)
                  {
                        Console.WriteLine("Exception reported: {0}", ex);
                  }
                  finally
                  {
                        parameters.Dispose();
                        stream.Dispose();
                        tiff.Dispose();
                  }
            }
      }
}
zimmer9Asked:
Who is Participating?

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

x
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.

chaauCommented:
According to MSDN Image.SaveAdd() method exists from version 1.1. Why can't you use it with version 4.0? Where does it fail? Can you show us the errors?
zimmer9Author Commented:
Ok, I got my code to compile now as follows:

Do you know how I can resolve the following error:

Error      1      The name 'MergeImages' does not exist in the current context      U:\Visual Studio 2010\Projects\Citi1\Citi1\Program.cs      582      17      Citi1
zimmer9Author Commented:
The error is in the following "foreach" loop when I try and call the new function MergeImages:

 foreach (Image image in newImage)
            {
                MergeImages(image, "L:\\test1.tiff");                                
            }          

I have attached my source code.
multipletiff.txt
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

chaauCommented:
if you put it inside the static class Extensions as per the code above then you need to use
 
Extensions.MergeImages(image, "L:\\test1.tiff");          

Open in new window

If you used the whole  EE_Q28711383 namespace then you need to use:
 
 EE_Q28711383.Extensions.MergeImages(image, "L:\\test1.tiff");          

Open in new window

chaauCommented:
BTW, you do not need to use foreach. The MergeImages() method accepts the IEnumerable object as a first parameter.
Just replace the lines:
foreach (Image image in newImage)
            {
                MergeImages(image, "L:\\test1.tiff");                                
            }          

Open in new window

with this line:
                MergeImages(newImage, "L:\\test1.tiff");                                

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
zimmer9Author Commented:
Thanks so very much. You were extremely helpful. )
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
C#

From novice to tech pro — start learning today.