Form1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
using System.IO;
namespace TestWinform
{
public partial class Form1 : Form
{
private static PixelFormat[] indexedPixelFormats = { PixelFormat.Undefined, PixelFormat.DontCare,
PixelFormat.Format16bppArgb1555, PixelFormat.Format1bppIndexed, PixelFormat.Format4bppIndexed,
PixelFormat.Format8bppIndexed };
public Form1()
{
InitializeComponent();
}
private void btnNoThirdSide_Click(object sender, EventArgs e)
{
//BitMiracle.GetTiffProperty(@"C:\test\mergeTif\1.tif");
this.Test2();
}
private void Test3()
{
try
{
List<string> lstTest = new List<string>();
lstTest.Add(@"C:\test\mergeTif\1.tif");
lstTest.Add(@"C:\test\mergeTif\2.tif");
List<Bitmap> lstImage = new List<Bitmap>();
foreach (var temp in lstTest)
{
Image currentImage = Image.FromFile(temp);
Bitmap bmp = new Bitmap(currentImage.Width, currentImage.Height, PixelFormat.Format1bppIndexed);
lstImage.Add(bmp);
}
string outputFile = @"C:\test\mergeTif\mergedImage.tiff";
MergeTiff.MergeTiffs(lstImage, outputFile);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Test2()
{
try
{
DateTime dt1 = DateTime.Now;
ArrayList al = new ArrayList();
//al.Add(@"C:\test\mergeTif\1.tif");
//al.Add(@"C:\test\mergeTif\2.tif");
//al.Add(@"C:\test\mergeTif\4.tif");
//al.Add(@"C:\test\mergeTif\5.tif");
////al.Add(@"C:\test\mergeTif\3.tif");
////al.Add(@"C:\test\mergeTif\4.tif");
////al.Add(@"C:\test\mergeTif\5.tif");
//al.Add(@"C:\test\mergeTif\6.tif");
//al.Add(@"C:\test\mergeTif\7.tif");
//al.Add(@"C:\test\mergeTif\8.tif");
//al.Add(@"C:\test\mergeTif\9.tif");
//al.Add(@"C:\test\mergeTif\10.tif");
//al.Add(@"C:\test\mergeTif_18\2.tif");
//al.Add(@"C:\test\mergeTif_18\31.tif");
//al.Add(@"C:\test\mergeTif_18\44.tif");
//al.Add(@"C:\test\mergeTif_18\4.tif");
al.Add(@"C:\test\mergeTif_18\1.tif");
al.Add(@"C:\test\mergeTif_18\11.tif");
string outputFile = @"C:\test\mergeTif_18\mergedImage24.tif";
TiffUtity.JoinTiffImages(al, outputFile, EncoderValue.CompressionCCITT4);
DateTime dt2 = DateTime.Now;
TimeSpan ts = dt2 - dt1;
MessageBox.Show("cost toatl time is:" + ts.Seconds + " s and " + ts.Milliseconds + " ms");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Test1()
{
try
{
List<string> lstTest = new List<string>();
lstTest.Add(@"C:\test\mergeTif\1.tif");
lstTest.Add(@"C:\test\mergeTif\2.tif");
lstTest.Add(@"C:\test\mergeTif\3.tif");
lstTest.Add(@"C:\test\mergeTif\4.tif");
string outputFile = @"C:\test\mergeTif\mergedImage.tiff";
MergeTiffFiles(lstTest, outputFile);
MessageBox.Show("done");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public static void MergeTiffFiles(List<string> inputFilePaths, string outputFilePath)
{
System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.SaveFlag;
EncoderParameters ep = new EncoderParameters(2);
ep.Param[0] = new EncoderParameter(enc, (long)EncoderValue.MultiFrame);
ep.Param[1] = new EncoderParameter(Encoder.Compression, (long)EncoderValue.CompressionCCITT3);
string testImage = @"C:\test\mergeTif\3.tiff";
using (Image firstImage = Image.FromFile(inputFilePaths[0]))
{
if (IsPixelFormatIndexed(firstImage.PixelFormat))
{
Bitmap bmp = new Bitmap(firstImage.Width, firstImage.Height, PixelFormat.Format32bppArgb);
using (Graphics g = Graphics.FromImage(bmp))
{
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.Low;
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default;
g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.Default;
//g.DrawImage(firstImage, 0, 0);
foreach (string inputFilePath in inputFilePaths)
{
if (inputFilePath != inputFilePaths[0])
{
using (Image currentImage = Image.FromFile(inputFilePath))
{
int x = firstImage.Width;
int y = 0; // 根据需要调整合并的位置
g.DrawImage(currentImage, new Point(x, y));
firstImage.Save(outputFilePath, ImageFormat.Tiff);
firstImage.SaveAdd(currentImage, ep);
}
}
}
}
}
else
{
using (Graphics graphics = Graphics.FromImage(firstImage))
{
foreach (string inputFilePath in inputFilePaths)
{
if (inputFilePath != inputFilePaths[0])
{
using (Image currentImage = Image.FromFile(inputFilePath))
{
int x = firstImage.Width;
int y = 0; // 根据需要调整合并的位置
graphics.DrawImage(currentImage, new Point(x, y));
firstImage.Save(outputFilePath, ImageFormat.Tiff);
}
}
}
}
}
}
}
private static bool IsPixelFormatIndexed(PixelFormat imgPixelFormat)
{
foreach (PixelFormat pf in indexedPixelFormats)
{
if (pf.Equals(imgPixelFormat)) return true;
}
return false;
}
private void btnMergeNoThirdSideWithArray_Click(object sender, EventArgs e)
{
try
{
var sourceFolder = @"C:\test\mergeTif";
var targetFilePath = @"C:\test\mergeTif\merged.tiff";
var files = new List<byte[]>();
foreach (var file in Directory.GetFiles(sourceFolder, "*.tif?"))
{
files.Add(File.ReadAllBytes(file));
}
var targetFileData = TiffHelper.MergeTiff(files.ToArray());
File.WriteAllBytes(targetFilePath, targetFileData);
}
catch (Exception ex)
{
throw ex;
}
}
private void btnMergeWithBitMiracle2_Click(object sender, EventArgs e)
{
try
{
DateTime dt1 = DateTime.Now;
//string[] pngFiles = { @"C:\test\mergeTif\11.png", @"C:\test\mergeTif\22.png", @"C:\test\mergeTif\33.png" };
string[] pngFiles = Directory.GetFiles(@"C:\test\mergeTif\test2\");
var lst = BitMiracle.GetbitmapsFromPnglist(pngFiles);
string outputTifFile = @"C:\test\mergeTif\merged22.tif";
BitMiracle.Jpegs2Tiff(lst, outputTifFile, 15);
DateTime dt2 = DateTime.Now;
TimeSpan ts = dt2 - dt1;
this.lblMessage.Text = "cost total time:" + ts.TotalSeconds + "seconds";
MessageBox.Show("done");
//ImageMerger.MergePngToTif(pngFiles, outputTifFile);
}
catch (Exception ex)
{
throw ex;
}
}
private void btnMergeWithBitMiracle_Click(object sender, EventArgs e)
{
try
{
DateTime dt1 = DateTime.Now;
//Bitmap bt1 = new Bitmap(@"C:\test\mergeTif\1.tiff");
//Bitmap bt2 = new Bitmap(@"C:\test\mergeTif\2.tiff");
//string[] bmps = new string[] { @"C:\test\mergeTif_18\2.tif", @"C:\test\mergeTif_18\4.tif", @"C:\test\mergeTif_18\310.tif" };
//string[] bmps = new string[] { @"C:\test\mergeTif_18\441.jpg", @"C:\test\mergeTif_18\311.jpg", @"C:\test\mergeTif_18\2.tif" };
//1: 将tif文件分解,并获取分解后的路径
//string[] sourceFiles1 = new string[] { @"C:\test\mergeTif_18\1.tif", @"C:\test\mergeTif_18\2.tif",
// @"C:\test\mergeTif_18\3.tif", @"C:\test\mergeTif_18\4.tif",@"C:\test\mergeTif_18\5.tif",@"C:\test\mergeTif_18\11.tif" };
////string[] sourceFiles1 = new string[] { @"C:\test\mergeTif_18\211.tif", @"C:\test\mergeTif_18\411.tif", @"C:\test\mergeTif_18\1.tif" };
//string outFilePath = TiffUtity.SplitTif(sourceFiles1);
////2:将分解后的图片进行jpeg的压缩
//string outFileForCompress = @"C:\test\mergeTif_18\temp";
//string[] sourceFiles = Directory.GetFiles(outFilePath);
//TiffUtity.VaryQualityLevel2(sourceFiles, outFileForCompress);
//string dirPath = @"C:\test\mergeTif_18\temp";
//ArrayList al = new ArrayList();
string dirPath = @"C:\test\mergeTif_18\temp";
string[] bmps = Directory.GetFiles(dirPath);
//string[] bmps = new string[] { @"C:\test\mergeTif\1.tiff", @"C:\test\mergeTif\2.tiff", @"C:\test\mergeTif\3.tif" };
//var result = BitMiracle.GetbitmapsFromTifflist(bmps);
string outputFilePath = @"C:\test\mergeTif_18\mertest.tif";
BitMiracle.Jpegs2Tiff(bmps, outputFilePath, 20);
DateTime dt2 = DateTime.Now;
TimeSpan ts = dt2 - dt1;
this.lblMessage.Text = "cost total time:" + ts.TotalSeconds + "seconds";
MessageBox.Show("done");
}
catch (Exception ex)
{
throw ex;
}
}
private void btnConvertTiffToPngWithEmgu_Click(object sender, EventArgs e)
{
try
{
DateTime dt1 = DateTime.Now;
string[] lstFile = Directory.GetFiles(@"C:\test\mergeTif\");
string outputDirectory = @"C:\test\mergeTif\";
string outputFileName = "mergedResult.tif";
BitMiracle.ConvertImageByEmgu(lstFile, outputDirectory, outputFileName);
DateTime dt2 = DateTime.Now;
TimeSpan ts = dt2 - dt1;
this.lblMessage.Text = "cost total time:" + ts.TotalSeconds + "seconds";
MessageBox.Show("done");
}
catch (Exception ex)
{
throw;
}
}
private void ConvertImageByEmgu()
{
try
{
//string tiffFilePath = @"C:\test\mergeTif\1.tiff"; // TIFF文件路径
//using (Image image = Image.FromFile(tiffFilePath))
//{
// int totalImage = image.GetFrameCount(FrameDimension.Page);
// for (int i = 0; i < totalImage; i++)
// {
// image.SelectActiveFrame(FrameDimension.Page, i);
// using (Bitmap bmp = new Bitmap(image))
// {
// string pngFilePath = $@"C:\test\mergeTif\test\frame_{i + 1}.png"; // PNG文件路径
// bmp.Save(pngFilePath, ImageFormat.Png);
// }
// }
//}
//BitMiracle.ConvertImageByEmgu();
}
catch (Exception ex)
{
throw ex;
}
}
private void btnAppendTifNoThirdSide_Click(object sender, EventArgs e)
{
try
{
ArrayList al = new ArrayList();
//al.Add(@"C:\test\mergeTif\1.tif");
//al.Add(@"C:\test\mergeTif\2.tif");
string filePath = @"C:\test\mergeTif\2.tif";
string targetFilePath = @"C:\test\mergeTif\1.tif";
//string outputFile = @"C:\test\mergeTif\mergedImage2.tiff";
TiffUtity.AppendToTiff(filePath, targetFilePath);
MessageBox.Show("done");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnConvert8Bitto1Bit_Click(object sender, EventArgs e)
{
try
{
this.lblMessage.Text = "";
string sourceFile = @"C:\test\mergeTif_18\31.tif";
string outFile = @"C:\test\mergeTif_18\310.tif";
TiffUtity.Convert8BitTo1Bit(sourceFile, outFile);
this.lblMessage.Text = "done";
}
catch (Exception ex)
{
throw ex;
}
}
private void btnSplitTif_Click(object sender, EventArgs e)
{
try
{
this.lblMessage.Text = "";
string sourceFile = @"C:\test\mergeTif_18\211.tif";
string outFilePath = @"C:\test\mergeTif_18";
TiffUtity.SplitTif(sourceFile, out outFilePath);
this.lblMessage.Text = "done";
}
catch (Exception ex)
{
throw ex;
}
}
private void btnCompressionWithJpeg_Click(object sender, EventArgs e)
{
try
{
this.lblMessage.Text = "";
string sourceFile = @"C:\test\mergeTif_18\411.tif";
string outFile = @"C:\test\mergeTif_18\4111.tif";
TiffUtity.VaryQualityLevel(sourceFile, outFile);
this.lblMessage.Text = "done";
}
catch (Exception ex)
{
throw ex;
}
}
private void button1_Click(object sender, EventArgs e)
{
try
{
//1: 将tif文件分解,并获取分解后的路径
string[] sourceFiles1 =new string[] { @"C:\test\mergeTif_18\1.tif", @"C:\test\mergeTif_18\211.tif", @"C:\test\mergeTif_18\411.tif" };
string outFilePath = TiffUtity.SplitTif(sourceFiles1);
//2:将分解后的图片进行jpeg的压缩
string outFileForCompress = @"C:\test\mergeTif_18\temp";
string[] sourceFiles = Directory.GetFiles(outFilePath);
TiffUtity.VaryQualityLevel2(sourceFiles, outFileForCompress);
//3:将压缩后的图片合并成tif
this.lblMessage.Text = "done";
}
catch (Exception ex)
{
throw ex;
}
}
private void button2_Click(object sender, EventArgs e)
{
try
{
string dirPath = @"C:\test\mergeTif_18\temp";
ArrayList al = new ArrayList();
string[] files = Directory.GetFiles(dirPath);
foreach (var tmpFile in files)
{
al.Add(tmpFile);
}
string outputFile = @"C:\test\mergeTif_18\mergedImage24.tif";
TiffUtity.JoinTiffImages(al, outputFile, EncoderValue.CompressionLZW);
this.lblMessage.Text = "done";
}
catch (Exception ex)
{
throw ex;
}
}
}
}