본문 바로가기
C# /WindowsForm

[C#] 특정폴더안의 이미지 파일들을 Tiff 파일로 Merge 예제

by Hwoarang757 2017. 2. 7.

[C#] 특정폴더안의 이미지 파일들을 Tiff 파일로 Merge 예제

 

특정 폴더안의 이미지만 존재시 해당 폴더안에 Merge.tif 라는 파일을 생성하여 Merge 하는 예제입니다.

 

Merge 대상이 Tiff 파일의 경우 첫장만 병합됩니다 -_-;;

 

내용이 부족해 죄송합니다.

 

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

namespace ImageMergeTest
{
    class Program
    {
        static void Main(string[] args)
        {

            Console.WriteLine("이미지가 존재하는 폴더를 입력 하세요");
            string imgDir =  Console.ReadLine();
            string saveFullPath = imgDir + "\\merge.tif";

            Image image = null;
            Image mergeImage = null;

            DirectoryInfo dir = new DirectoryInfo(imgDir);

            ImageCodecInfo codecInfo = null;
            codecInfo = ImageCodecInfo.GetImageEncoders().Where(a => a.MimeType == "image/tiff").FirstOrDefault();

            System.Drawing.Imaging.Encoder encoderFrame = System.Drawing.Imaging.Encoder.SaveFlag;
            System.Drawing.Imaging.Encoder encoderCompress = System.Drawing.Imaging.Encoder.Compression;
            EncoderParameters parameters = new EncoderParameters(2);
            parameters.Param[0] = new EncoderParameter(encoderFrame, (long)EncoderValue.MultiFrame);
            parameters.Param[1] = new EncoderParameter(encoderCompress, (long)EncoderValue.CompressionLZW);

            foreach (FileInfo f in dir.GetFiles())
            {
                if (!File.Exists(saveFullPath))
                {
                    mergeImage = Image.FromFile(f.FullName);
                    //FrameDimension frameDim = new FrameDimension(mergeImage.FrameDimensionsList[0]);
                    //mergeImage.SelectActiveFrame(frameDim, 1);
                    mergeImage.Save(saveFullPath, codecInfo, parameters);
                }
                else
                {
                    if (mergeImage == null)
                    {
                        mergeImage = Image.FromFile(saveFullPath);

                        FrameDimension frameDimention = new FrameDimension(mergeImage.FrameDimensionsList[0]);
                        MemoryStream[] ms = new MemoryStream[mergeImage.GetFrameCount(frameDimention)];
                        for (int i = 0; i < mergeImage.GetFrameCount(frameDimention); i++) {
                            ms[i] = new MemoryStream();
                            mergeImage.SelectActiveFrame(frameDimention,i);
                            mergeImage.Save(ms[i],codecInfo,parameters);
                        }
                        
                        mergeImage.Dispose();
                        File.Delete(saveFullPath);

                        for(int i = 0 ; i < ms.Length ; i ++) {
                           if(i == 0) {
                             mergeImage = Image.FromStream(ms[i]);
                             mergeImage.Save(saveFullPath, codecInfo, parameters);
                           }
                           else {
                             image = Image.FromStream(ms[i]);
                             parameters.Param[0] = new EncoderParameter(encoderFrame, (long)EncoderValue.FrameDimensionPage);
                             parameters.Param[1] = new EncoderParameter(encoderCompress, (long)EncoderValue.CompressionLZW);
                             mergeImage.SaveAdd(image,parameters);
                             image.Dispose();
                           }
                        }
                    }

                    if (Path.GetFileName(f.FullName).ToUpper() != Path.GetFileName(saveFullPath).ToUpper())
                    {
                        image = Image.FromFile(f.FullName);
                        parameters.Param[0] = new EncoderParameter(encoderFrame, (long)EncoderValue.FrameDimensionPage);
                        parameters.Param[1] = new EncoderParameter(encoderCompress, (long)EncoderValue.CompressionLZW);
                        mergeImage.SaveAdd(image, parameters);
                    }
                }
                if (image != null)
                    image.Dispose();
            }



        }
    }
}