如何使用 Windows 服务中的代码获取 tiff 文件中的页数

发布于 2025-01-04 00:29:43 字数 699 浏览 0 评论 0原文

我有一个 Windows 服务,尝试使用 System.Drawing 命名空间中的 Image 类获取 TIFF 文件中的页数。

    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;
    }

但微软文档 http://msdn.microsoft.com/en-us /library/system.drawing.aspx 表示不要在 Windows 服务中使用 System.Drawing。他们建议使用 Windows 成像组件。我下载它是为了看看它是否有办法获取 TIFF 文件中的页数,但安装时出错。所以我还没有答案。

我很好奇其他人使用什么来获取 Windows 服务中 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最初的梦 2025-01-11 00:29:43

这是一个通用的 C 函数,用于计算 TIFF 文件中的页数

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

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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文