MavidDeyers
MavidDeyers

Reputation: 290

C# LibTiff.Net - MultiTIFF change Tag-Values

i want to set or change some Tags in a Multi-TIFF-File with LibTiff.net. So im currently switching through the Sub-Images with SetDirectory(), updating some fields and checking out with the CheckpointDirectory()-Function. When im doing this, only the first image in the generated Multi-TIFF-File is visible, the others are completely black. That even happens without changing any tag with the following code. What point am i missing ?

If i set the CheckoutDirectory function outside the For loop, all the pictures will be displayed as desired, but I want to change the tags of all SubTiffs, not just the ones of the last one.

public static void setRequiredTags(string outputFilePath)
    {
        using (Tiff image_MultiTIFF = Tiff.Open(outputFilePath, "a"))
        {
            for (int i = 0; i < image_MultiTIFF.NumberOfDirectories(); i++)
            { 
                // Load the Next Sub-TIFF
                   image_MultiTIFF.SetDirectory((short)i);  
                // setting custom tag  
                // image_MultiTIFF.SetField(TiffTag.PAGENUMBER, i, image_MultiTIFF.NumberOfDirectories());
                // image_MultiTIFF.SetField(TiffTag.DATETIME, DateTime.Now); 

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

Upvotes: 0

Views: 524

Answers (1)

Bobrovsky
Bobrovsky

Reputation: 14246

Most probable cause of the black images is image data corruption.

When you change something in tags and save the changes using CheckpointDirectory or WriteDirectory, the library writes new data at the same location in file. If new directory data is larger than the old data, some part of the next image might be overwritten (i.e corrupted).

The only way to avoid corruption in all cases is to use RewriteDirectory.

Please note that each call to RewriteDirectory creates a copy of directory. The old version of the directory stays in the file.

Upvotes: 1

Related Questions