Copy TIFF image by using Libtiff

北慕城南 提交于 2020-01-06 11:55:07

问题


      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.


回答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..




回答2:


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());


来源:https://stackoverflow.com/questions/24740141/copy-tiff-image-by-using-libtiff

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!