0
      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'"
                  }
               }
           }
        }
    }

I am opening a file reading the tag values and writing them to another TIFF file. The problem occurs in the SetField function. I tried debugging it, everything seems fine, cant quite figure out, why it is throwing an error.

TaW
  • 53,122
  • 8
  • 69
  • 111
harsha217
  • 72
  • 1
  • 11

2 Answers2

1

In LibTiff that I'm using SetField method is expecting the actual values, not the FieldValue objects. So you can try something like this:

newiimage.SetField(tag, value.Select(v => v.Value).ToArray());
OpenMinded
  • 496
  • 3
  • 10
  • Thanks for the reply. So, what is the data type of "value" in ur solution. Because I was trying to extract and copy all the tags, "value" could be anything, right? Pardon me if my approach seems naive – harsha217 Jul 14 '14 at 16:56
1

According to the Doc of the GetField method there are both real and pseudo-tags in its return set.

TiffTag.JPEGQUALITY is an example of a pseudo-tag.

You can't set the pseudo-tags, so you must exlude them from the write-loop.

The list of Well-known tags may help you to write a filter..

It may also be necessary to write out the tags that can't be written to make the filter more complete.

I do not know the precise nature of the pseudo-tag but given the openness of the tiff format, you are probably well-advised to include error handling for unexepected late-comers..

TaW
  • 53,122
  • 8
  • 69
  • 111
  • ok thanks for the info....so, the copying of a tiff image by copying tags is not as simple as i thought – harsha217 Jul 14 '14 at 17:43
  • Well, for a start, you may try to simply wrap the SetField in a try-catch block and log/write out the failing tags to check, if they are indeed the pseudo-tags. – TaW Jul 14 '14 at 17:52
  • Your idea helped me filter them. So, I have one more question. How to set pseudo tags(Compression,Photometric,ResUnit,etc in my image) ? We should set all of them manually ? – harsha217 Jul 14 '14 at 19:17
  • 1
    I have not studied the docs in full; from the little I read, I believe that these pseudo-tags are read-only. I believe the library routines create them for your convenience or for internal use and they are not really part of the tiff-file. So you can't and you don't need to set them. Their values probably are derived from other tags or image data. – TaW Jul 14 '14 at 21:51