java多页tiff文件合并,将多个多页tiff图像合并到单个tiff C#

In my scenario I have 3 or more multi-page tiff images which I need to merge into a single tiff image.

Below is the the code I have tried. It merges in to a single tiff image but only with first page of all tiff images.

private static void MergeTiff(string[] sourceFiles)

{

string[] sa = sourceFiles;

//get the codec for tiff files

ImageCodecInfo info = null;

foreach (ImageCodecInfo ice in ImageCodecInfo.GetImageEncoders())

if (ice.MimeType == "image/tiff")

info = ice;

//use the save encoder

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)

{

if (frame == 0)

{

MemoryStream ms = new MemoryStream(File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, @"C:\Data_Warehouse\SVNRepository\CD.BNS.W5555.LT45555C.D180306.T113850.Z0101\", s)));

pages = (Bitmap)Image.FromStream(ms);

var appDataPath = @"C:\Data_Warehouse\SVNRepository\Tiffiles\";

var filePath = Path.Combine(appDataPath, Path.GetRandomFileName() + ".tif");

//save the first frame

pages.Save(filePath, info, ep);

}

else

{

//save the intermediate frames

ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

try

{

MemoryStream mss = new MemoryStream(File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, @"C:\Data_Warehouse\SVNRepository\CD.BNS.W5555.LT45555C.D180306.T113850.Z0101\", s)));

Bitmap bm = (Bitmap)Image.FromStream(mss);

pages.SaveAdd(bm, ep);

}

catch (Exception e)

{

//LogError(e, s);

}

}

if (frame == sa.Length - 1)

{

//flush and close.

ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.Flush);

pages.SaveAdd(ep);

}

frame++;

}

}

I need to join multiple tiff images with all pages from each tiff image. Please advise!

Thanks

EDIT: Updated from below answer

if (frame == 0)

{

MemoryStream ms = new MemoryStream(File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, @"C:\OMTest\Working\", s)));

pages = (Bitmap)Image.FromStream(ms);

var appDataPath = @"C:\Data_Warehouse\SVNRepository\Tiffiles\";

var filePath = Path.Combine(appDataPath, Path.GetRandomFileName() + ".tif");

//save the first frame

pages.Save(filePath, info, ep);

//Save the second frame if any

int frameCount1 = pages.GetFrameCount(FrameDimension.Page);

if (frameCount1 > 1)

{

for (int i = 1; i < frameCount1; i++)

{

ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

pages.SelectActiveFrame(FrameDimension.Page, i);

pages.SaveAdd(pages, ep);

}

}

}

else

{

//save the intermediate frames

ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.FrameDimensionPage);

try

{

MemoryStream mss = new MemoryStream(File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, @"C:\OMTest\Working\", s)));

Bitmap bm = (Bitmap)Image.FromStream(mss);

int frameCount = bm.GetFrameCount(FrameDimension.Page);

for (int i = 0; i < frameCount; i++)

{

bm.SelectActiveFrame(FrameDimension.Page, i);

pages.SaveAdd(bm, ep);

}

}

catch (Exception e)

{

//LogError(e, s);

}

}

解决方案

You need to select the active frame to ensure you are getting all pages on the TIFF. In your code you need to get the count of frames and loop through these.

The code in your else block might look something like this:

MemoryStream mss = new MemoryStream(File.ReadAllBytes(Path.Combine(Environment.CurrentDirectory, @"C:\Data_Warehouse\SVNRepository\CD.BNS.W5555.LT45555C.D180306.T113850.Z0101\", s)));

Bitmap bm = (Bitmap)Image.FromStream(mss);

int frameCount = bm.GetFrameCount(FrameDimension.Page);

for(int i=0;i

bm.SelectActiveFrame(FrameDimension.Page, i);

pages.SaveAdd(bm, ep);

}

You may have to tweak it as I haven't tested it.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值