using System; using System.Collections; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq;namespace Common {/// <summary>/// Title Tif文件合并类/// Author:ward/// </summary>public class Tiff{/// <summary> /// 获取图像页数 /// </summary> /// <param name="imagePath"></param> /// <returns></returns> public static int GetPageNumber(string imagePath){using (Image image = Bitmap.FromFile(imagePath)){Guid objGuid = image.FrameDimensionsList[0];FrameDimension objDimension = new FrameDimension(objGuid);return image.GetFrameCount(objDimension);}}/// <summary> /// 将给定的文件 拼接输出到指定的tif文件路径 /// </summary> /// <param name="imageFiles">文件路径列表</param> /// <param name="outFile">拼接后保存的 tif文件路径</param> /// <param name="compressEncoder">压缩方式,建议CompressionCCITT4</param> public static void JoinTiffImages(ArrayList imageFiles, string outFile, EncoderValue compressEncoder){//如果只有一个文件,直接复制到目标 if (imageFiles.Count == 1){File.Copy((string)imageFiles[0], outFile, true);return;}Encoder enc = Encoder.SaveFlag;EncoderParameters 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(); //释放资源 return;}/// <summary> /// 拼接两个tif文件 保存到文件2中 /// </summary> /// <param name="filePath">tif文件1</param> /// <param name="targetFile">tif文件2</param> public static void AppendToTiff(string filePath, string targetFile){ArrayList list = new ArrayList(); //保存所有 tif文件路径 #region 分割tif文件1 string tempDirectory1 = string.Empty;list.AddRange(SplitTif(filePath, out tempDirectory1));#endregion#region 分割tif文件2 string tempDirectory2 = string.Empty;list.AddRange(SplitTif(targetFile, out tempDirectory2));#endregion//2. 拼接所有tif页 //2.1 删除原目标文件 File.Delete(targetFile);//2.2 拼接 并按原路径生成tif文件 JoinTiffImages(list, targetFile, EncoderValue.CompressionCCITT3);//3. 删除临时目录 DirectoryInfo di2 = new DirectoryInfo(tempDirectory2);di2.Delete(true);DirectoryInfo di1 = new DirectoryInfo(tempDirectory1);di1.Delete(true);}/// <summary> /// 将给定文件 分割成多个tif文件 到临时目录下 /// </summary> /// <param name="targetFile">目标文件</param> /// <param name="tempDirectory">临时目录路径,删除用</param> /// <returns>分割后多个文件路径集合</returns> public static ArrayList SplitTif(string targetFile, out string tempDirectory){ArrayList list = new ArrayList();using (Image img = Image.FromFile(targetFile)){Guid guid = img.FrameDimensionsList[0];System.Drawing.Imaging.FrameDimension dimension = new System.Drawing.Imaging.FrameDimension(guid);int nTotFrame = img.GetFrameCount(dimension); //tif总页数 int nLoop = 0; //索引 //生成临时目录 存放 单tif页 tempDirectory = Path.Combine(Path.GetDirectoryName(targetFile), 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);//保存 单tif页 string newfilePath = Path.Combine(tempDirectory, nLoop.ToString() + ".tif");img.Save(newfilePath, info, ep);//将路径存入 list中 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");}} }
Tif文件合并类
转载于:https://www.cnblogs.com/lee2011/p/6170238.html
相关文章:

ubuntu /boot 空间清理
本文引用自 blog.csdn.net/yypony/article/details/17260153方法: 1: 在终端下察看已经安装的旧的内核: ctrlaltt——>进入终端——>输入命令:dpkg --get-selections|grep linux给 /boot文件目录分配空间的时候,…

Swift default参数
swift 支持默认参数,在声明方法的时候,可以给某个参数制定一个默认的使用值,当没有传入值的时候,使用默认的参数,当传入值存在的时候,使用对应的传入值 import UIKitvar str "Hello, playground&quo…

vs2017 open从v_宣布#Open2017,这是面向开发人员的除夕直播流
vs2017 open从vHere are a few reasons to stay home this New Year’s Eve:这是除夕之夜留在家里的一些理由: It’s the worst day of the year for fatal drunk driving deaths 这是致命的酒后驾车致死的一年中最糟糕的一天 It’s crowded 拥挤 It’s freaking c…

Laravel Predis Error while reading line from the server.
问题 Laravel说明文档中的 Redis 发布与订阅案例,命令行运行php artisan redis:subscribe 到60s自动断开并报错 [Predis\Connection\ConnectionException]Error while reading line from the server. [tcp://127.0.0.1:6379]解决 在config/database.php配置文件中&a…

android 带边框的圆角按钮
新建buttonstyle.xml 代码如下 <?xml version"1.0" encoding"UTF-8"?> <layer-list xmlns:android"http://schemas.android.com/apk/res/android"> <!-- 连框颜色值 --><item> <shape> <solid andr…

Swift 字面量表达
字面量是一个很强大的特性,对于缩短代码很有帮助 // // 1 字面量就是 简洁明了指出自己的类型并且为变量赋值的的值 // tom false 称之为字面量 let dog:String "tom"; let ok false;// Array 和 Dictionary 赋值的时候也是使用的字面量 let animals:Ar…

韩国文档的编码方式_像韩国学生学习英语一样学习编码
韩国文档的编码方式by Stephen Mayeux斯蒂芬马约(Stephen Mayeux) 像韩国学生学习英语一样学习编码 (Learn to code like a Korean student learns English) If this is the first you’ve heard of me, then there’s only one thing you need to know: I am an ESL Teacher …

mysql乱码解决
在 /etc/my.cnf加上下面二句: skip-character-set-client-handshakecharacter-set-server utf8转载于:https://www.cnblogs.com/sweetXiaoma/p/6170979.html

LoaderManager使用具体解释(三)---实现Loaders
这篇文字将介绍Loader<D>类,而且介绍自己定义Loader的实现。这是本系列的第三篇文章。一:Loaders之前世界二:了解LoaderManager三:实现Loaders四:实例:AppListLoader重中之重,假设你还没有…

Swift 条件编译,编译标记
1 swift 中的条件编译跟OC中的形式是相同的 #if DEBUGself.navigationView.backgroundColor Color_ff3b30;#elseself.navigationView.backgroundColor Color_main;#endif条件可以接受 os(MacOS) ,参数iOS tvOS等平台 arch(),参数为平台架构组合 arm64 ,i386 swift(),参数为版…

代码简介:向圣诞老人和他的精灵学习Google Analytics(分析)
Here are three stories we published this week that are worth your time:这是我们本周发布的三个值得您关注的故事: Learn Google Analytics from Santa and his elves: 12 minute read 向圣诞老人和他的精灵学习Google Analytics(分析): 阅读12分钟…

生物信息大数据数据库(NCBI、EBI、UCSC、TCGA)
想系统的学习生信数据库可以先看一下北大的公开课,有一章专门讲的数据库与软件: 1-生物信息学:导论与方法 北大\10 生物信息数据库及软件资源 一个优秀的生信开发者能够解决如下问题: 如何鉴定一个重要的且没有被解决的生物学问题…

Dispatch 执行ABC任务,执行完成之后刷新UI,指定任务D
在swift中分组管理异步任务的方式 1 group enter 和 leave 进行同步管理 func method1() {// 创建一个组 ,要是在一个控制器中去规划请求顺序,则这个组要是全局的组let group DispatchGroup();let queue DispatchQueue.global();//let imgsArr [&qu…

关于页游垂直同步的若干问题
这个问题要从人眼感觉抖动的原因来分析第一种情况是常说的屏幕撕裂,就是垂直同 步的事情,可以简单理解为显存的数据更新跟屏幕的绘制刷新缺少同步,一次屏幕刷新的结果可能是多次显存更新的片段集合,这种情况只能使用更接近垂直同步…

javascript函数式_JavaScript中的函数式编程—结合实际示例(第1部分)
javascript函数式by rajaraodv通过rajaraodv JavaScript中的函数式编程—结合实际示例(第1部分) (Functional Programming In JavaScript — With Practical Examples (Part 1)) Functional Programming(FP) can change the way you program for the better. But it’s hard t…
简介子窗口控件(api)
子窗口控件 壹佰软件开发小组 整理编译 回忆第七章的CHECKER程序。这些程序显示了矩形网格。当您在一个矩形中按下鼠标按键时,该程序就画一个x;如果您再按一次鼠标按键,那么x就消失。虽然这个程序的CHECKER1和CHECKER2版本只使用一个主窗口…

【MongoDB学习之一】初始MongoDB
环境 MongoDB4.0 win7_x64 CentOS6.5_x64 一、MongoDB简介 (1)MongoDB使用C开发。 (2)MongoDB 旨在为WEB应用提供可扩展的高性能数据存储解决方案。 (3)MongoDB 将数据存储为一个文档。MongoDB是一个基于分布式文件存储的数据库。 (4)MongoDB使用BSON作为数据存储…

swift 和 oc中检测textfield是否输入数字
iOS 开发中用来检测输入框是否输入的是纯数字 Swift 版本 // 代理方法func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {//判断输入的是否是数字 否则无效var cs CharacterSet();// 获取除…

开源项目贡献者_如何吸引新的贡献者加入您的开源项目
开源项目贡献者by Shubheksha通过Shubheksha 如何吸引新的贡献者加入您的开源项目 (How to attract new contributors to your open source project) It’s hard to attract contributors to your FOSS project — especially contributors who are new to open source.很难吸…

滑动轮播图实现最后一张图片无缝衔接第一张图片
原理:使用insertBefore和insertAfter方法调整图片顺序。 测试:firefox/chrome/IE11正常 已知不足:每次播放均使用了一次insertBefore和insertAfter,可考虑在最后一张图的时候将前几张图片整体后移。以后有空再优化。 1、HTML结构 …

一个完整的Installshield安装程序实例—艾泽拉斯之海洋女神出品(三) --高级设置一...
一个完整的Installshield安装程序实例—艾泽拉斯之海洋女神出品(三) --高级设置一 原文:一个完整的Installshield安装程序实例—艾泽拉斯之海洋女神出品(三) --高级设置一上一篇:一个完整的安装程序实例—艾泽拉斯之海…

数据结构,堆和栈和队列的概念
数据结构,堆和栈和队列的概念 1 什么是数据结构 数据结构是计算机存储,组织数据的反复改。数据结构是指相互之间存在的一种或多种特定关系的数据元素集合。 2 数据结构的逻辑结构 1 集合结构,元素都是孤立存在的 2 线性结构 ,…

电子白板 矢量 编码_当涉及白板编码采访时,请记住准备
电子白板 矢量 编码by Andy Tiffany通过安迪蒂芙尼(Andy Tiffany) 当涉及白板编码采访时,请记住准备 (When it comes to whiteboard coding interviews, remember to PREP) PREP is a mnemonic I created to help you remember the steps involved in solving whit…

机器学习实战笔记(Python实现)-03-朴素贝叶斯
--------------------------------------------------------------------------------------- 本系列文章为《机器学习实战》学习笔记,内容整理自书本,网络以及自己的理解,如有错误欢迎指正。 源码在Python3.5上测试均通过,代码及…

SQLite.swift的简单使用
使用cocoapod 来进行引入 pod ‘SQLite.swift’ // // SQLiteTool.swift // CreateLectureForSwift // // Created by coder on 2019/6/25. // Copyright © 2019 AlexanderYeah. All rights reserved. // import Foundation import SQLite // id let id Expression(“…

Cypress USB开发文档列表(积累中)
CyUSB.chm(pdf) \Cypress\Cypress Suite USB 3.4.7\Driver Cypress CyUSB.sys Programmers Reference 内容: CyUsb.sys、CyUsb.inf 驱动程序介绍,如何绑定设备到驱动程序,以IOCTL Interface、CYIOCTL.Hd的解释为主要内容的编程指导,主…

对象冒充_使用您的精神探照灯进行冒充冒名顶替综合症
对象冒充by Jaime J. Rios由Jaime J. Rios “Stop that imposter! Seize them!”“停止冒名顶替者! 抓住他们!” I first spotted my imposter two years ago. It happened when I began learning how to code.两年前,我第一次发现了冒名顶…

grep 函数
linux grep命令1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来。grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户。 2.格式gr…

iOS weak 自动置为nil的实现
1 weak 自动置为nil的实现 runtime 维护了一个Weak表,weak_table_t 用于存储指向某一个对象的所有Weak指针。Weak表其实是一个哈希表, key是所指对象的地址,value是weak指针的地址的数组。 在对象回收的时候,就会在weak表中进…

iOS 缓存策略
Github https://github.com/gaosboy/kache https://github.com/swtlovewtt/WTRequestCenter https://github.com/hans007/CacheFile Image https://github.com/youger/UIImageView-ASIImageCache转载于:https://www.cnblogs.com/hl666/p/3931182.html