使用Libtiff复制TIFF图像
本文关键字:图像 TIFF 复制 Libtiff 使用 | 更新日期: 2023-09-27 18:12:55
using (Tiff iimage = Tiff.Open("new.tif", "r"))
{
Tiff newiimage = Tiff.Open("newnew.tif", "w");
if (image == null)
{
MessageBox.Show("Could not open incoming image");
return;
}
using (StreamWriter writer = new StreamWriter("EnumerateTiffTags.txt"))
{
short numberOfDirectories = iimage.NumberOfDirectories();
for (short d = 0; d < numberOfDirectories; ++d)
{
iimage.SetDirectory((short)d);
for (ushort t = ushort.MinValue; t < ushort.MaxValue; ++t)
{
TiffTag tag = (TiffTag)t;
var value = iimage.GetField(tag);
if (value != null)
{
for (int j = 0; j < value.Length; j++)
{
writer.WriteLine("{0}", tag.ToString());
writer.WriteLine("{0} : {1}",
value[j].Value.GetType().ToString(), value[j].ToString());
}
newiimage.SetField(tag, value);// this line is giving me..
// an error "Unable to cast object of type 'BitMiracle.LibTiff.Classic.FieldValue[]'
// to type 'System.IConvertible'"
}
}
}
}
}
我正在打开一个文件,读取标签值并将它们写入另一个TIFF文件。SetField函数出现问题。处理步骤我试着调试它,一切似乎都很好,不能完全弄清楚,为什么它抛出一个错误。
根据GetField方法的文档,它的返回集中有真实标签和伪标签。
TiffTag。JPEGQUALITY是一个伪标签的例子。
不能设置伪标记,因此必须将它们排除在写循环之外。
众所周知的标签列表可以帮助你编写一个过滤器…
为了使过滤器更完整,也可能需要写出不能写的标签。
我不知道伪标签的确切性质,但是考虑到tiff格式的开放性,您最好为意外的迟来的标签包括错误处理。
在LibTiff中,我使用SetField
方法期望实际值,而不是FieldValue
对象。所以你可以尝试这样做:
newiimage.SetField(tag, value.Select(v => v.Value).ToArray());