Link to home
Start Free TrialLog in
Avatar of zimmer9
zimmer9Flag for United States of America

asked on

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();
                  }
            }
      }
}
Avatar of chaau
chaau
Flag of Australia image

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?
Avatar of zimmer9

ASKER

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
Avatar of zimmer9

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of chaau
chaau
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of zimmer9

ASKER

Thanks so very much. You were extremely helpful. )