Wednesday, March 6, 2013

C# Code To Split, Merge Tiff Files


Split the TIFF File
---------------------
filepath--- The Path of the Source TIFF File need to Split.

DestTiffile ---The Path of the Destination Folder, the splited files need to be saved.



        protected void SplitTiff(string filepath)
        {
            int activePage;
            int pages;

            string DestTiffile = dirPath + "\\Images";

            System.Drawing.Image image = System.Drawing.Image.FromFile(filepath + ".tif");
            
            pages = image.GetFrameCount( System.Drawing.Imaging.FrameDimension.Page);
            
            for (int index = 0; index < pages; index++)
            {
            
                activePage = index + 1;
                image.SelectActiveFrame(System.Drawing.Imaging.FrameDimension.Page, index);
                image.Save(DestTiffile + "\\TIFSplit\\TifFile_" + activePage.ToString() + ".tif");

            }
       
        }







Merge the Multiple TIFF Files into one File
--------------------------------------------------
SourceFiles--- The Path of the Source TIFF Files need to Merge.

str_DestinationPath---The Path of the Destination Folder, the Merged file need to be saved.



       public void mergeTiffPages(string str_DestinationPath, string[] sourceFiles)
        {

            System.Drawing.Imaging.ImageCodecInfo codec = null;

            foreach (System.Drawing.Imaging.ImageCodecInfo cCodec in System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders())
            {
                if (cCodec.CodecName == "Built-in TIFF Codec")
                    codec = cCodec;
            }

            try
            {
              
                System.Drawing.Imaging.EncoderParameters imagePararms = new System.Drawing.Imaging.EncoderParameters(1);
                imagePararms.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)System.Drawing.Imaging.EncoderValue.MultiFrame);
                
                if (sourceFiles.Length == 1)
                {
                    System.IO.File.Copy((string)sourceFiles[0], str_DestinationPath, true);
                
                }
                else if (sourceFiles.Length >= 1)
                {
                    System.Drawing.Image DestinationImage = (System.Drawing.Image)(new System.Drawing.Bitmap((string)sourceFiles[0]));
                   
                    DestinationImage.Save(str_DestinationPath, codec, imagePararms);

                    imagePararms.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)System.Drawing.Imaging.EncoderValue.FrameDimensionPage);


                    for (int i = 0; i < sourceFiles.Length-1; i++)
                    {
                        System.Drawing.Image img = (System.Drawing.Image)(new System.Drawing.Bitmap((string)sourceFiles[i]));
                       
                        DestinationImage.SaveAdd(img, imagePararms);
                        img.Dispose();
                    }

                    imagePararms.Param[0] = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.SaveFlag, (long)System.Drawing.Imaging.EncoderValue.Flush);
                    DestinationImage.SaveAdd(imagePararms);
                    imagePararms.Dispose();
                    DestinationImage.Dispose();

                }
           
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }


        }