在C#中合并多个TIFF文件

时间:2013-10-12 13:42:08

标签: c#

我正在尝试将多个.tif文件合并为一个,但合并后,新.tif文件的图像质量非常低。

如何提高质量?

我希望新的合并文件质量为原始质量。我正在使用此代码合并tif文件

   string[] sa = path;
        ImageCodecInfo info = null;
        foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())
            if (ice.MimeType == "image/tiff")
                info = ice;
        Encoder enc = Encoder.SaveFlag;
        EncoderParameters ep = new EncoderParameters(1);
        ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
        Bitmap pages = null;
        int frame = 0;
        foreach (string s in sa)
        {
            //  using (FileStream fileStream = System.IO.File.Open(s, FileMode.Open))
            {
                if (frame == 0)
                {
                    pages = (Bitmap)Image.FromFile(s);
                    //save the first frame
                    pages.Save(filepath, info, ep);
                }
                else
                {
                    //save the intermediate frames
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);
                    Bitmap bm = (Bitmap)Image.FromFile(s);
                    pages.SaveAdd(bm, ep);
                }
                if (frame == sa.Length - 1)
                {
                    //flush and close.
                    ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);
                    pages.SaveAdd(ep);
                }
                frame++;
            }
        }

1 个答案:

答案 0 :(得分:0)

不是100%肯定这个,但我相信默认情况下使用G3编码多帧TIFF。只是尝试一下,改变一下:

Encoder enc = Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(1);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);

到此:

Encoder enc = Encoder.SaveFlag;
Encoder encComp = Encoder.Encoder.Compression;
EncoderParameters ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(encComp, (long)EncoderValue.CompressionLZW);

再试一次(您也可以使用CompressionNone代替CompressionLZW,但LZW无损,因此不应降低质量)