c# 拆分tif文件 比如tif文件里面有10个图片,需要1-2 当做一个整体拆分,3-4当做一个整体拆分,以此类推 9-10 当做一个整体, 这个例子相当于拆分成了5个tif文件
tiff切片、合并、转换
http://www.voidcn.com/article/p-kloikjag-bab.html
SplitTif,分割一个tif到多个tif;JoinTiffImages,合并多个图片到tif
[Test]
public void SplitTiff()
{
var targetFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "黑控励志.tiff");
var images = TiffHelper.SplitTif(targetFile, out var tempDirectory);
var pageDirectory = Path.Combine(Path.GetDirectoryName(targetFile) ?? throw new InvalidOperationException(), Path.GetFileNameWithoutExtension(targetFile));
if (!Directory.Exists(pageDirectory)) Directory.CreateDirectory(pageDirectory);
var pageSize = 2;
var pageCount = images.Count / 2 + 1;
for (int i = 0; i < pageCount; i++)
{
var fileName = Path.Combine(pageDirectory, $"page_{i}.tif");
var files = images.Skip(pageSize * i).Take(pageSize).ToList();
TiffHelper.JoinTiffImages(files, fileName, EncoderValue.CompressionCCITT3);
}
//清理
Directory.Delete(tempDirectory, true);
}
public class TiffHelper
{
/// <summary>
/// 获取图像页数
/// </summary>
/// <param name="imagePath"></param>
/// <returns></returns>
public static int GetPageNumber(string imagePath)
{
using (var image = Image.FromFile(imagePath))
{
var objGuid = image.FrameDimensionsList[0];
var objDimension = new FrameDimension(objGuid);
return image.GetFrameCount(objDimension);
}
}
/// <summary>
/// 将给定的文件 拼接输出到指定的tif文件路径
/// </summary>
/// <param name="imageFiles">文件路径列表</param>
/// <param name="outFile">拼接后保存的 tif文件路径</param>
/// <param name="compressEncoder">压缩方式</param>
public static void JoinTiffImages(List<string> imageFiles, string outFile, EncoderValue compressEncoder = EncoderValue.CompressionCCITT3)
{
//如果只有一个文件,直接复制到目标
if (imageFiles.Count == 1)
{
File.Copy((string) imageFiles[0], outFile, true);
return;
}
var enc = Encoder.SaveFlag;
var ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(enc, (long) EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(Encoder.Compression, (long) compressEncoder);
Bitmap pages = null;
int frame = 0;
ImageCodecInfo info = GetEncoderInfo("image/tiff");
foreach (string strImageFile in imageFiles)
{
if (frame == 0)
{
pages = (Bitmap) Image.FromFile(strImageFile);
//保存第一个tif文件 到目标处
pages.Save(outFile, info, ep);
}
else
{
//保存好第一个tif文件后,其余 设置为添加一帧到 图像中
ep.Param[0] = new EncoderParameter(enc, (long) EncoderValue.FrameDimensionPage);
Bitmap bm = (Bitmap) Image.FromFile(strImageFile);
pages.SaveAdd(bm, ep);
bm.Dispose();
}
if (frame == imageFiles.Count - 1)
{
//flush and close.
ep.Param[0] = new EncoderParameter(enc, (long) EncoderValue.Flush);
pages.SaveAdd(ep);
}
frame++;
}
pages?.Dispose(); //释放资源
}
/// <summary>
/// 拼接两个tif文件 保存到文件2中
/// </summary>
/// <param name="filePath">tif文件1</param>
/// <param name="targetFile">tif文件2</param>
public static void AppendToTiff(string filePath, string targetFile)
{
var list = new List<string>(); //保存所有 tif文件路径
list.AddRange(SplitTif(filePath, out var tempDirectory1));
list.AddRange(SplitTif(targetFile, out var tempDirectory2));
File.Delete(targetFile);
JoinTiffImages(list, targetFile, EncoderValue.CompressionCCITT3);
var di2 = new DirectoryInfo(tempDirectory2);
di2.Delete(true);
var di1 = new DirectoryInfo(tempDirectory1);
di1.Delete(true);
}
/// <summary>
/// 将给定文件 分割成多个tif文件 到临时目录下
/// </summary>
/// <param name="targetFile">目标文件</param>
/// <param name="tempDirectory">临时目录路径,删除用</param>
/// <returns>分割后多个文件路径集合</returns>
public static List<string> SplitTif(string targetFile, out string tempDirectory)
{
var list = new List<string>();
using (var img = Image.FromFile(targetFile))
{
Guid guid = img.FrameDimensionsList[0];
var dimension = new System.Drawing.Imaging.FrameDimension(guid);
int nTotFrame = img.GetFrameCount(dimension); //tif总页数
int nLoop; //索引
//生成临时目录 存放 单tif页
tempDirectory = Path.Combine(Path.GetDirectoryName(targetFile) ?? throw new InvalidOperationException(), Guid.NewGuid().ToString());
if (!Directory.Exists(tempDirectory))
{
Directory.CreateDirectory(tempDirectory);
}
EncoderParameters ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(Encoder.SaveFlag, (long) EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(Encoder.Compression, (long) EncoderValue.CompressionCCITT3); //压缩方式 CompressionCCITT3主要用于传真
ImageCodecInfo info = GetEncoderInfo("image/tiff");
for (nLoop = 0; nLoop < nTotFrame; nLoop++)
{
img.SelectActiveFrame(dimension, nLoop);
string newfilePath = Path.Combine(tempDirectory, nLoop + ".tif");
img.Save(newfilePath, info, ep);
list.Add(newfilePath);
}
}
return list;
}
/// <summary>
/// 获取支持的编码信息
/// </summary>
/// <param name="mimeType">协议描述</param>
/// <returns>图像编码信息</returns>
private static ImageCodecInfo GetEncoderInfo(string mimeType)
{
ImageCodecInfo[] encoders = ImageCodecInfo.GetImageEncoders();
for (int j = 0; j < encoders.Length; j++)
{
if (encoders[j].MimeType == mimeType)
return encoders[j];
}
throw new Exception(mimeType + " mime type not found in ImageCodecInfo");
}
}
谢谢大佬的回答,大佬是先拆分成10个,然后再合并吧 不能直接2个2个拆分实现么
对tif文件进行解析为多张图片,在去按自己的格式生成tif文件.
您的思路也是先拆分 再合并吧,如果要实现2个2个拆分呢
@wangleping: 还是解析出来多张图片,然后组合生成tif,这样应该是简单的方式
该问题已自行解决