Arv*_*wen 5 c# tags tiff libtiff.net
我似乎找不到任何有关如何使用LibTiff.Net库删除tiff标签的文档.我喜欢图书馆,但这种方法对我需要做的很重要.有一次,我希望我可以设置一个标签并将其值设置为空.我曾希望这会起作用,但这是消极的.
任何人都知道如何使用LibTiff.Net库删除tiff标签?
注意:首先,这可能看起来像是一个很大的答案,但我想确保无论是谁看到这个,都会看到我创建的所有“专有类,以保持所有内容都保持干净。为了保持答案与为了提供更多信息,我将仅粘贴 DeleteTiffTags 方法的代码。其余代码可以通过此处下载。
现在说说好东西......我最终花了大约一天的时间来实现这一点,这要归功于伟大的 stackoverflow 社区回答的各种问题。我在我的一个类中编写了两个小(非常详细)方法来删除 tiff 标签。第一个是删除给定标签的列表,第二个是删除单个标签,该标签适用于上述方法。另外,在此示例中,我添加了几行来支持我的自定义 tiff 标签...它们前面都会带有 //ADDED 注释。
课程:
public static class TIFFTAGS - 该类是主类,只需通过执行 TIFFTAGS.DeleteTiffTags() 之类的操作即可调用;由于它是静态类,因此无需创建它的对象来使用它的方法。
private 类 TIFFTAGS_PAGE - 该类是驻留在 TIFFTAGS 类内部的私有类。它的目的是包含 tiff 中可能存在的所有页面的所有单页信息。它是私有的,仅用于内部目的。
public class TIFFTAGS_TAG - 这是我为了将标签包装成更符合我的喜好而创建的类。使用标准标记类型名称,例如 ASCII、SHORT、LONG 和 RATIONAL。
方法/函数:
TagExtender() - 这个小宝石是一个回调函数,允许您将自定义标签实际保留在 tiff 中。如果没有它,当您删除任何标签(即使您只删除一个)时,所有自定义标签都会消失。
DeleteTiffTags() - 这是删除标签列表的主要方法。只需传入一个 ushort 标签编号列表,所有标签编号都会被删除。请记住,不使用 TagExtender 功能将导致您的自定义标签 失效!
DeleteTiffTag() - 这仅用于删除单个 tiff 标签。它调用DeleteTiffTags()来处理繁重的工作。
public static bool DeleteTiffTags(string sFileName, List<ushort> ushortTagNumbers)
{
//Deletes a list of tiff tag from the given image
//Returns true if successful or false if error occured
//Define variables
List<TIFFTAGS_PAGE> ttPage = new List<TIFFTAGS_PAGE>();
//Check for empty list
if (ushortTagNumbers.Count == 0) return false;
try
{
//ADDED
m_lTagsToWrite = new List<TIFFTAGS_TAG>();
m_lTagsToWrite.Add(new TIFFTAGS_TAG("", 38001, Convert.ToString("")));
m_lTagsToWrite.Add(new TIFFTAGS_TAG("", 38002, Convert.ToString("")));
m_parentExtender = Tiff.SetTagExtender(TagExtender);
//Open the file for reading
using (Tiff input = Tiff.Open(sFileName, "r"))
{
if (input == null) return false;
//Get page count
int numberOfDirectories = input.NumberOfDirectories();
//Go through all the pages
for (short i = 0; i < numberOfDirectories; ++i)
{
//Set the page
input.SetDirectory(i);
//Create a new tags dictionary to store all my tags
Dictionary<ushort, FieldValue[]> dTags = new Dictionary<ushort, FieldValue[]>();
//Get all the tags for the page
for (ushort t = ushort.MinValue; t < ushort.MaxValue; ++t)
{
TiffTag tag = (TiffTag)t;
FieldValue[] tagValue = input.GetField(tag);
if (tagValue != null)
{
dTags.Add(t, tagValue);
}
}
//Check if the page is encoded
bool encoded = false;
FieldValue[] compressionTagValue = input.GetField(TiffTag.COMPRESSION);
if (compressionTagValue != null)
encoded = (compressionTagValue[0].ToInt() != (int)Compression.NONE);
//Create a new byte array to store all my image data
int numberOfStrips = input.NumberOfStrips();
byte[] byteImageData = new byte[numberOfStrips * input.StripSize()];
int offset = 0;
//Get all the image data for the page
for (int n = 0; n < numberOfStrips; ++n)
{
int bytesRead;
if (encoded)
bytesRead = input.ReadEncodedStrip(n, byteImageData, offset, byteImageData.Length - offset);
else
bytesRead = input.ReadRawStrip(n, byteImageData, offset, byteImageData.Length - offset);
//Add to the offset keeping up with where we are
offset += bytesRead;
}
//Save all the tags, image data, and height, etc for the page
TIFFTAGS_PAGE tiffPage = new TIFFTAGS_PAGE();
tiffPage.Height = input.GetField(TiffTag.IMAGELENGTH)[0].ToInt();
tiffPage.Tags = dTags;
tiffPage.PageData = byteImageData;
tiffPage.Encoded = encoded;
tiffPage.StripSize = input.StripSize();
tiffPage.StripOffset = input.GetField(TiffTag.STRIPOFFSETS)[0].ToIntArray()[0];
ttPage.Add(tiffPage);
}
}
//Open the file for writing
using (Tiff output = Tiff.Open(sFileName + "-new.tif", "w"))
{
if (output == null) return false;
//Go through all the pages
for (short i = 0; i < ttPage.Count(); ++i)
{
//Write all the tags for the page
foreach (KeyValuePair<ushort, FieldValue[]> tagValue in ttPage[i].Tags)
{
//Write all the tags except the one's needing to be deleted
if (!ushortTagNumbers.Contains(tagValue.Key))
{
TiffTag tag = (TiffTag)tagValue.Key;
output.GetTagMethods().SetField(output, tag, tagValue.Value);
}
}
//Set the height for the page
output.SetField(TiffTag.ROWSPERSTRIP, ttPage[i].Height);
//Set the offset for the page
output.SetField(TiffTag.STRIPOFFSETS, ttPage[i].StripOffset);
//Save page data along with tags
output.CheckpointDirectory();
//Write each strip one at a time using the same orginal strip size
int numberOfStrips = ttPage[i].PageData.Length / ttPage[i].StripSize;
int offset = 0;
for (int n = 0; n < numberOfStrips; ++n)
{
//Write all the image data (strips) for the page
if (ttPage[i].Encoded)
//output.WriteEncodedStrip(n, byteStrip, offset, byteStrip.Length - offset);
output.WriteEncodedStrip(0, ttPage[i].PageData, offset, ttPage[i].StripSize - offset);
else
output.WriteRawStrip(n, ttPage[i].PageData, offset, ttPage[i].StripSize - offset);
//Add to the offset keeping up with where we are
offset += ttPage[i].StripOffset;
}
//Save the image page
output.WriteDirectory();
}
}
//ADDED
Tiff.SetTagExtender(m_parentExtender);
}
catch
{
//ADDED
Tiff.SetTagExtender(m_parentExtender);
//Error occured
return false;
}
//Return success
return true;
}
Run Code Online (Sandbox Code Playgroud)