Iori的工具箱 之 C# 实现多个图片合为一个Tiff

功能:


合并多个图片为一个TIFF文件
效果图:


第二页

Iori的工具箱 之 C# 实现多个图片合为一个Tiff_第1张图片

第三页

Iori的工具箱 之 C# 实现多个图片合为一个Tiff_第2张图片


功能函数实现:

     public bool SaveMultipage(Image[] bmp, string location, string type)
        {
            if (bmp != null)
            {
                try
                {
                    File.Delete(location);
                    
                    var codecInfo = getCodecForstring(type);

                    if (bmp.Length == 1)
                    {
                        var iparams = new EncoderParameters(1);
                        var iparam = Encoder.Compression;
                        var iparamPara = new EncoderParameter(iparam, (long)(EncoderValue.CompressionNone));

                        iparams.Param[0] = iparamPara;
                        bmp[0].Save(location, codecInfo, iparams);
                    }
                    else if (bmp.Length > 1)
                    {
                        // Save the first page (frame).
                        var encoderParam1 = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.MultiFrame);
                        var encoderParam2 = new EncoderParameter(Encoder.Compression,
                                                                 (long) EncoderValue.CompressionNone);

                        var encoderParams = new EncoderParameters(2);
                        encoderParams.Param[0] = encoderParam1;
                        encoderParams.Param[1] = encoderParam2;

                        bmp[0].Save(location, codecInfo, encoderParams);

                        for (int i = 1; i < bmp.Length; i++)
                        {
                            if (bmp[i] == null)
                                break;

                            encoderParams.Param[0] = encoderParam1;
                            encoderParams.Param[1] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.FrameDimensionPage);
                            bmp[0].SaveAdd(bmp[i], encoderParams);
                        }

                        encoderParams.Param[0] = new EncoderParameter(Encoder.SaveFlag, (long)EncoderValue.Flush);
                        bmp[0].SaveAdd(encoderParams);
                    }
                    return true;
                }
                catch (Exception ee)
                {
                    throw new Exception(ee.Message + "  Error in saving as multipage ");
                }
            }

            return false;
        }

        private ImageCodecInfo getCodecForstring(string type)
        {
            var info = ImageCodecInfo.GetImageEncoders();

            return (info.Select(t => new {t, enumName = type})
                        .Where(@t1 => @t1.t.FormatDescription.Equals(@t1.enumName))
                        .Select(@t1 => @t1.t))
                .FirstOrDefault();
        }


代码说明:参数:图片数组,保存文件的位置,保存类型


单元测试代码实现:

  [TestMethod]
        public void Multiple_Image_Into_1Tiff_With_MultiplePages()
        {
            const string multipleImgPath = "C:\\Users\\Public\\Pictures\\Sample Pictures";
            var files = new DirectoryInfo(multipleImgPath).GetFiles("*.jpg", SearchOption.TopDirectoryOnly);
            Assert.IsTrue(files.Length > 1);
            var imgs = files.Select(f => Image.FromFile(f.FullName));
            SaveMultipage(imgs.ToArray(), "C:\\multiplePageTiff.tiff", "TIFF");

            const string singleImgPath = @"C:\Users\{your_pc_name}\Pictures\SingleImg";
            files = new DirectoryInfo(singleImgPath).GetFiles("*.jpg", SearchOption.TopDirectoryOnly);
            Assert.IsTrue(files.Length == 1);

            imgs = files.Select(f => Image.FromFile(f.FullName));
            SaveMultipage(imgs.ToArray(), "C:\\multiplePageTiff_Single.tiff", "TIFF");
        }


代码说明:

1.测试多个图片的转化

2.测试单个突破的转化


你可能感兴趣的:(C#)