سؤال

I want to simply update the EXIF_USERCOMMENT field on a tif image I am creating.

The call to SetField fails. Not sure what I'm doing wrong.

Here is my over-simplistic code.

Any help would be appreciated.

{
    Tiff tiffdoc = Tiff.Open("C:\\temp\\~tempCapture.tif","rw");

    bool bSetField = tiffdoc.SetField(TiffTag.EXIF_USERCOMMENT, "test comment field");

    tiffdoc.WriteDirectory();
    tiffdoc.Close();
}
هل كانت مفيدة؟

المحلول

Unfortunately, it looks like LibTiff.Net can only read EXIF tags and can not write them (original libtiff can't write EXIF tags either).

There is a discussion in the libtiff mailing list about why it is so. Here are some quotes from the discussion:

// FIXME -- we don't currently support writing of EXIF fields.  TIFF
// in theory allows it, using a custom IFD directory, but at
// present, it appears that libtiff only supports reading custom
// IFD's, not writing them.

Leonard Rosenthol:

I don't really think that libTIFF really wants to start down the
metadata "rabbit hole"... 

Bob Friesenhahn:

I do agree with Leonard Rosenthol that libtiff should not be in the 
business of dealing with EXIF private IFD tags (even though it 
somewhat does already).

As for the Unknown tag EXIF_USERCOMMENT, you should read an EXIF directory first. The library will add EXIF tags to its list of known tags before reading the EXIF directory and won't issue the error about unknown tags later.

But the library still won't be able to write EXIF tags to file.

EDIT:

You have some options in case you only want to store some information in your file and not require it to be stored in the EXIF_USERCOMMENT tag.

You can use IMAGEDESCRIPTION tag for your task. Below is sample code that uses this tag. Please note that the code uses different params for Open method and also uses RewriteDirectory instead of WriteDirectory.

string fileName = "C:\\temp\\~tempCapture.tif";
using (Tiff tiffdoc = Tiff.Open(fileName, "a"))
{
    tiffdoc.SetDirectory(0);
    bool bSetField = tiffdoc.SetField(TiffTag.IMAGEDESCRIPTION, "test comment field");
    tiffdoc.RewriteDirectory();
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top