Home All Groups Group Topic Archive Search About
Author
4 Jul 2006 8:44 AM
Crespo
hi,everyone
   I have a question about merging images. I want to merge serval tiff
images into one file using C#,that means the final file has multiple
frames.My problem is that I want each frame in the final file is of
single-strip,but when I want to merge serval images which has been of
singe-strip,the final file my codes produced contains images which are all
of multiple-strips. My codes is listed below:

        public static bool MergeTifs(ref string mergedFile,ArrayList
fileList)
        {
           int counts = fileList.Count;
           if (counts < 1)
           {
               return false;
           }
           try
           {
                EncoderParameters eps=new EncoderParameters(2);

                eps.Param[0] = new

EncoderParameter(Encoder.SaveFlag,(long)EncoderValue.MultiFrame);
                eps.Param[1] = new EncoderParameter(Encoder.Compression,
(long)EncoderValue.CompressionCCITT3);

                System.Drawing.Image mergedImage =
System.Drawing.Image.FromFile((string)fileList[0]);

                //Get the correct encoder from the list of available
encoders

                ImageCodecInfo[] infos = ImageCodecInfo.GetImageEncoders();

                int n=0;
                while(infos[n].MimeType!="image/tiff")
                     n++;

                //save the first page
                System.Drawing.Image imageToAdd = null;
                mergedImage.Save(mergedFile,infos[n],eps);
                Bitmap firstBmp = new Bitmap((string)fileList[0]);
                int first_num = firstBmp.GetFrameCount(FrameDimension.Page);
                for(int j = 1; j < first_num; j++)
                {
                    firstBmp.SelectActiveFrame(FrameDimension.Page,j);
                     eps.Param[0]=new
EncoderParameter(Encoder.SaveFlag,(long)EncoderValue.FrameDimensionPage);
                     mergedImage.SaveAdd(firstBmp,eps);
                }
                firstBmp.Dispose();
                for(int i = 1; i< counts; i++)
                {
                     string fileToAdd = (string)fileList[i];
                     imageToAdd =  System.Drawing.Image.FromFile(fileToAdd);
                     int num =
imageToAdd.GetFrameCount(FrameDimension.Page);
                     for(int k = 0; k < num; k++)
                     {

imageToAdd.SelectActiveFrame(FrameDimension.Page,k);
                          eps.Param[0]=new
EncoderParameter(Encoder.SaveFlag,(long)EncoderValue.FrameDimensionPage);
                          mergedImage.SaveAdd(imageToAdd,eps);
                      }
                     imageToAdd.Dispose();
                }
                eps.Param[0]=new

EncoderParameter(Encoder.SaveFlag,(long)EncoderValue.Flush);
                 mergedImage.SaveAdd(eps);
                mergedImage.Dispose();
                return true;
           }
           catch
           {
                return false;
           }
      }

     the first parameter is the file name of the final file.
    Could anyone kindly help me? Thank you in advance.

Crespo

2006-07-03

AddThis Social Bookmark Button