43 lines
1.3 KiB
C#
43 lines
1.3 KiB
C#
using BitMiracle.LibTiff.Classic;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace XPrint.Image
|
||
{
|
||
public class TiffSaver
|
||
{
|
||
public static void SaveTiff(Tiff tiff, string outputPath)
|
||
{
|
||
if (tiff == null) throw new ArgumentNullException(nameof(tiff));
|
||
|
||
try
|
||
{
|
||
// 确保输出目录存在
|
||
string directory = Path.GetDirectoryName(outputPath)!;
|
||
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
||
{
|
||
Directory.CreateDirectory(directory);
|
||
}
|
||
|
||
// 写入文件
|
||
tiff.WriteDirectory(); // 保存当前IFD目录
|
||
tiff.Close(); // 关闭文件句柄
|
||
|
||
// 若需覆盖原文件,需手动替换(LibTiff不支持直接覆盖)
|
||
if (File.Exists(outputPath))
|
||
{
|
||
File.Delete(outputPath);
|
||
}
|
||
File.Move(tiff.FileName(), outputPath); // 移动临时文件到目标路径
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
throw new IOException($"保存TIFF文件失败: {ex.Message}");
|
||
}
|
||
}
|
||
}
|
||
}
|