Hi Phil,
Length, in bytes, of TIFF image data can be calcuated from TIFF image metadata.
Here is an example that shows how to get length, in bytes, of TIFF image data:
Code: Select all
using System;
using Vintasoft.Imaging.Codecs.ImageFiles.Tiff;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(string.Format("'strips.tif', page 0, image data length in bytes: {0}", GetTiffImageDataLengthInBytes("strips.tif", 0)));
Console.WriteLine(string.Format("'tiles.tif', page 0, image data length in bytes: {0}", GetTiffImageDataLengthInBytes("tiles.tif", 0)));
Console.WriteLine("Press any key...");
Console.ReadKey();
}
/// <summary>
/// Gets the length, in bytes, of TIFF image data.
/// </summary>
/// <param name="filename">Name of TIFF file.</param>
/// <param name="pageIndex">Zero-based index of page in TIFF file.</param>
/// <returns></returns>
static uint GetTiffImageDataLengthInBytes(string filename, int pageIndex)
{
// length, in bytes, of image data
uint imageDataLengthInBytes = 0;
// open TIFF file
using (TiffFile tiffFile = new TiffFile(filename))
{
// get TIFF tag collection of specified TIFF page
TiffTagCollection tags = tiffFile.Pages[pageIndex].IFD.Tags;
// get StripByteCounts tag
TiffTag stripByteCountsTag = tags.Find(TiffTagId.StripByteCounts);
// if tag is found image data are stored in strips
if (stripByteCountsTag != null)
{
object stripByteCountsTagData = stripByteCountsTag.Data;
uint[] stripByteCountTagAsUIntArray = stripByteCountsTagData as uint[];
if (stripByteCountTagAsUIntArray != null)
{
for (int i = 0; i < stripByteCountTagAsUIntArray.Length; i++)
imageDataLengthInBytes = stripByteCountTagAsUIntArray[i];
}
}
// get TileByteCounts tag
TiffTag tileByteCountsTag = tags.Find(TiffTagId.TileByteCounts);
// if tag is found image data are stored in tiles
if (tileByteCountsTag != null)
{
object tileByteCountsTagData = tileByteCountsTag.Data;
uint[] tileByteCountsTagAsUIntArray = tileByteCountsTagData as uint[];
if (tileByteCountsTagAsUIntArray != null)
{
for (int i = 0; i < tileByteCountsTagAsUIntArray.Length; i++)
imageDataLengthInBytes = tileByteCountsTagAsUIntArray[i];
}
}
}
return imageDataLengthInBytes;
}
}
}
Best regards, Alexander