XPrintServer/XPrint.Image/TiffSaver.cs
2025-11-16 19:33:01 +08:00

43 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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}");
}
}
}
}