首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LibTiff.NET追加模式错误?

LibTiff.NET追加模式错误?
EN

Stack Overflow用户
提问于 2012-03-20 22:59:00
回答 1查看 646关注 0票数 2

我最近开始使用LibTiff.NET来编写tiff IPTC标签,并在我这里的一些文件上发现了奇怪的行为。我使用的是LibTiff.NET二进制文件附带的示例代码,它可以很好地处理大多数图像,但在以下几行之后,一些文件会有图像数据损坏:

代码语言:javascript
复制
class Program
{
    private const TiffTag TIFFTAG_GDAL_METADATA = (TiffTag)42112;

    private static Tiff.TiffExtendProc m_parentExtender;

    public static void TagExtender(Tiff tif)
    {
        TiffFieldInfo[] tiffFieldInfo =
        {
            new TiffFieldInfo(TIFFTAG_GDAL_METADATA, -1, -1, TiffType.ASCII,
                              FieldBit.Custom, true, false, "GDALMetadata"),
        };

        tif.MergeFieldInfo(tiffFieldInfo, tiffFieldInfo.Length);

        if (m_parentExtender != null)
            m_parentExtender(tif);
    }

    public static void Main(string[] args)
    {
        // Register the extender callback
        // It's a good idea to keep track of the previous tag extender (if any) so that we can call it
        // from our extender allowing a chain of customizations to take effect.
        m_parentExtender = Tiff.SetTagExtender(TagExtender);

        string destFile = @"d:\00000641(tiffed).tif";

        File.Copy(@"d:\00000641.tif", destFile);

        //Console.WriteLine("Hello World!");

        // TODO: Implement Functionality Here
        using (Tiff image = Tiff.Open(destFile, "a"))
    {
        // we should rewind to first directory (first image) because of append mode
        image.SetDirectory(0);

        // set the custom tag 
        string value = "<GDALMetadata>\n<Item name=\"IMG_GUID\">" + 
            "817C0168-0688-45CD-B799-CF8C4DE9AB2B</Item>\n<Item" + 
            " name=\"LAYER_TYPE\" sample=\"0\">athematic</Item>\n</GDALMetadata>";
        image.SetField(TIFFTAG_GDAL_METADATA, value);

        // rewrites directory saving new tag
        image.CheckpointDirectory();
    }

    // restore previous tag extender
    Tiff.SetTagExtender(m_parentExtender);
        Console.Write("Press any key to continue . . . ");
        Console.ReadKey(true);
    }
}

打开后,我看到的大多是空白的白色图像或多条黑白线条,而不是已经写在那里的文本(我不需要读\写标签来产生这种行为)。我注意到,当图像已经有一个自定义标签(控制台窗口提示)或者其中一个标签的值不正确(控制台窗口显示'vsetfield:%pathToTiffFile%:‘%TagName%’的坏值0‘)时,就会发生这种情况。

原图:http://dl.dropbox.com/u/1476402/00000641.tif

LibTiff.NET之后的图片:http://dl.dropbox.com/u/1476402/00000641%28tiffed%29.tif

如果能提供任何帮助,我将不胜感激。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-23 02:04:04

对于以追加模式打开的文件,您可能不应使用CheckpointDirectory方法。请尝试使用RewriteDirectory方法。

它会重写目录,但不是像WriteDirectory()那样把它放在原来的位置(就像WriteDirectory()那样),而是把它们放在文件的末尾,纠正前一个目录或文件头中的指针指向它的新位置。当目录和指向数据的大小增长了时,这一点尤其重要,因此它不能放在旧位置的可用空间中。请注意,这将导致丢失以前使用的目录空间。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9789246

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档