Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get get number of pages in a tiff file with code in a Windows Service

Tags:

.net

tiff

I have a Windows service that tries to get the number of pages in a TIFF file using the Image class in the System.Drawing namespace.

    using System.Drawing;

    private int GetNumberOfPagesFromTiffFile(string filePath)
    {
        int pageCount = 0;
        Image Tiff = Image.FromFile(filePath);
        pageCount = Tiff.GetFrameCount(FrameDimension.Page);
        Tiff.Dispose(); 

        return pageCount;
    }

But the Microsoft documentation http://msdn.microsoft.com/en-us/library/system.drawing.aspx says not to use System.Drawing in a Windows service. They suggested using Windows Imaging Component. I downloaded it to see if it has a way to get the number of pages in a TIFF file, but I got an error installing it. So I don't have my answer yet.

I'm curious what others use to get the number of pages in a TIFF file in a Windows service.

like image 951
Clinton Bast Avatar asked Nov 20 '25 18:11

Clinton Bast


1 Answers

Here's a generic C function to count the number of pages in a TIFF file

uint16_t TIFFSHORT(unsigned char *buffer, bool bMotorola)
{
uint16_t s;

if (bMotorola)
   s = (buffer[0] << 8) + buffer[1];
else
   s = buffer[0] + (buffer[1] << 8);

return s;  
}

uint32_t TIFFLONG(unsigned char *buffer, bool bMotorola)
{
uint32_t l;

if (bMotorola)
   l = (buffer[0] << 24) + (buffer[1] << 16) + (buffer[2] << 8) + buffer[3];
else
   l = buffer[0] + (buffer[1] << 8) + (buffer[2] << 16) + (buffer[3] << 24);

return l;
}

int TIFFPageCount(char *filename)
{
    int i, iTags, IFD;
    int iOffset, iTotalPages;
    unsigned char buf[8];
    FILE *handle;
    bool bMotorola;
    int iFileSize;

    handle = fopen((char *)filename, "rb");
    if (handle == NULL)
       return 0;

    // get the file size
    fseek(handle, 0, SEEK_END);
    iFileSize = (int)ftell(handle);
    fseek(handle, 0, SEEK_SET);

    i = fread(buf, 1, 8, handle); // Read TIFF header and first IFD offset

    if (buf[0] != 'I' && buf[0] != 'M')
    {
        fclose(handle);
        return 0; // Not a TIFF file
    }
    bMotorola = (buf[0] == 'M');   // get the byte order
    IFD = TIFFLONG(&buf[4], bMotorola); // Read the first IFD pointer
    if (IFD < 8 || IFD > iFileSize) // corrupt file, don't process it
    {
        fclose(handle);
        return 0;
    }

    iTotalPages = 0;

    while(IFD != 0 && IFD < iFileSize-4) // count the number of pages
    {
        fseek(handle, IFD, SEEK_SET);
        fread(buf, 1, 2, handle); // get the number of tags in this page 
        iTags = TIFFSHORT(buf, bMotorola);  // Number of tags in this dir
        i = iTags * 12 + 2; // Offset to next IFD in chain
        if (iTags > 256) // Invalid header, abort
            break;
        fseek(handle, IFD+i, SEEK_SET);
        fread(buf, 1, 4, handle); // read the next IFD value; 0 means end of chain 
        IFD = TIFFLONG(buf, bMotorola);
        iTotalPages++;
    }
    fclose(handle);
    return iTotalPages;
} // TIFFPageCount()
like image 71
BitBank Avatar answered Nov 23 '25 08:11

BitBank



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!