1

注意:当创建一个从未被 LibTiff.Net 库更改过的新 tiff 图像时,我可以使用 LibTiff.Net 库毫无问题地添加/更改标签(第一次)。

无论如何,在我使用 LibTiff.Net 库更改 tiff 标签后,当我再次尝试更改它们时,图像已损坏。标签似乎很好,但图像本身显示为黑色图像(损坏)。

这是目前我在课堂上使用的标签扩展器......

private static Tiff.TiffExtendProc m_parentExtender;
private static List<TIFFTAGS_TAG> m_lTagsToWrite;

private static void TagExtender(Tiff tif)
{
    List<TiffFieldInfo> tfInfo = new List<TiffFieldInfo>();
    Int16 readCount = 0;
    Int16 writeCount = 0;
    bool okToChange = false;
    bool passCount = false;

    //Make sure to create a new tiff field info for all the tags to be written
    foreach (TIFFTAGS_TAG ttTag in m_lTagsToWrite)
    {
        switch (ttTag.TagType)
        {
            case TIFFTAGS_TAG_DATATYPE.ASCII:
                readCount = -1;
                writeCount = -1;
                okToChange = true;
                passCount = false;

                break;
            case TIFFTAGS_TAG_DATATYPE.LONG:
            case TIFFTAGS_TAG_DATATYPE.RATIONAL:
            case TIFFTAGS_TAG_DATATYPE.SHORT:
                readCount = 2;
                writeCount = 2;
                okToChange = false;
                passCount = true;

                break;
        }

        if (ttTag.TagType != TIFFTAGS_TAG_DATATYPE.UNKNOWN)
        {
            //Add the new item to the list
            tfInfo.Add(new TiffFieldInfo((TiffTag)ttTag.TagNumber, readCount, writeCount, (TiffType)ttTag.TagType, FieldBit.Custom, okToChange, passCount, ttTag.TagName));
        }
    }

    //Turn the list into an array again
    TiffFieldInfo[] tfInfoArray = tfInfo.ToArray();

    //Add the fields to the main field info area
    tif.MergeFieldInfo(tfInfoArray, tfInfoArray.Length);

    //Propergate the chain if needed
    if (m_parentExtender != null)
        m_parentExtender(tif);
}

这是目前我用来在给定 tiff 上设置 tiff 标签的函数

public static bool SetTiffTags(string sFileName, List<TIFFTAGS_TAG> ttTagsInfo)
{
    //Sets all the tiff tags in the image given.
    //Returns true if successful or false if error occured

    //Check if the file exists
    if (!File.Exists(sFileName))
    {
        //File does not exist
        return false;
    }

    try
    {
        //Set the tag info so that it can be carried over to the extender callback
        m_lTagsToWrite = new List<TIFFTAGS_TAG>();
        m_parentExtender = Tiff.SetTagExtender(TagExtender);
        m_lTagsToWrite = ttTagsInfo;

        using (Tiff tImage = Tiff.Open(sFileName, "a"))
        {
            if (tImage != null)
            {
                //Go to the first page
                tImage.SetDirectory(0);

                foreach (TIFFTAGS_TAG ttTag in ttTagsInfo)
                {
                    switch (ttTag.TagType)
                    {
                        case TIFFTAGS_TAG_DATATYPE.ASCII:
                            try
                            {
                                //string
                                string sValue = ttTag.Value.ToString();
                                tImage.SetField((TiffTag)ttTag.TagNumber, sValue);
                            }
                            catch
                            {
                                //Could not convert to a string
                                return false;
                            }
                            break;
                        case TIFFTAGS_TAG_DATATYPE.LONG:
                            try
                            {
                                //long
                                int[] iValues;
                                if (ttTag.Value.GetType().ToString() == "System.Int32")
                                {
                                    iValues = new int[] { (int)ttTag.Value };
                                }
                                else if (ttTag.Value.GetType().ToString() == "System.Int32[]")
                                {
                                    iValues = (int[])ttTag.Value;
                                }
                                else return false;
                                tImage.SetField((TiffTag)ttTag.TagNumber, iValues.Count(), iValues);
                            }
                            catch
                            {
                                //Could not convert to a long
                                return false;
                            }
                            break;
                        case TIFFTAGS_TAG_DATATYPE.SHORT:
                            try
                            {
                                //short
                                Int16[] iValues;
                                if (ttTag.Value.GetType().ToString() == "System.Int16")
                                {
                                    iValues = new Int16[] { (Int16)ttTag.Value };
                                }
                                else if (ttTag.Value.GetType().ToString() == "System.Int16[]")
                                {
                                    iValues = (Int16[])ttTag.Value;
                                }
                                else return false;
                                tImage.SetField((TiffTag)ttTag.TagNumber, iValues.Count(), iValues);
                            }
                            catch
                            {
                                //Could not convert to a short
                                return false;
                            }
                            break;
                        case TIFFTAGS_TAG_DATATYPE.RATIONAL:
                            try
                            {
                                //float
                                Single[] iValues;
                                if (ttTag.Value.GetType().ToString() == "System.Single")
                                {
                                    iValues = new Single[] { (Single)ttTag.Value };
                                }
                                else if (ttTag.Value.GetType().ToString() == "System.Single[]")
                                {
                                    iValues = (Single[])ttTag.Value;
                                }
                                else return false;
                                tImage.SetField((TiffTag)ttTag.TagNumber, iValues.Count(), iValues);
                            }
                            catch
                            {
                                //Could not convert to a single
                                return false;
                            }
                            break;
                        default:
                            //This type of tag is not supported so error out
                            return false;
                    }
                }

                //Write the information to the tiff
                tImage.CheckpointDirectory();
            }
            else
            {
                //Restore previous tag extender
                Tiff.SetTagExtender(m_parentExtender);

                return false;
            }
        }

        //Restore previous tag extender
        Tiff.SetTagExtender(m_parentExtender);
    }
    catch
    {
        //Restore previous tag extender
        Tiff.SetTagExtender(m_parentExtender);

        return false;
    }

    //Return success
    return true;
}
4

1 回答 1

1

快速查看您的代码让我认为问题的原因是调用tImage.CheckpointDirectory();

通过此调用,您可以有效地将目录数据写入文件,而无需考虑可能已经放在那里的内容。

我猜想通过调用写入的目录迟早会CheckpointDirectory开始占用比以前为文件中的目录数据分配的空间更多的空间,并且光栅数据的开头会被覆盖。

请尝试改用RewriteDirectory方法。

于 2012-08-20T14:34:11.327 回答