2012-02-08 9 views
3

System.Drawing名前空間のImageクラスを使用してTIFFファイル内のページ数を取得しようとするWindowsサービスがあります。Windowsサービスでコードを含む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; 
    } 

しかし、Microsoftのドキュメントhttp://msdn.microsoft.com/en-us/library/system.drawing.aspx WindowsサービスでSystem.Drawingを使用しないことを言います。彼らは、Windows Imaging Componentの使用を提案しました。 TIFFファイルのページ数を取得する方法があるかどうかを確認するためにダウンロードしましたが、インストール時にエラーが発生しました。だからまだ私の答えはありません。

私は、他の人がWindowsサービスのTIFFファイルのページ数を取得するのに興味があるのは興味があります。

+3

TIFFファイル内のページ数をカウントするために、一般的なC言語の関数ですか?それは非常にシンプルで、CreateFile()、ReadFile()以上のものを必要としません。 – BitBank

+0

BitBankに感謝します。 しかし、私はTIFFファイルについて何も知らなかったので、元々は "IFDチェーン"の意味を知らなかった。私は最終的にTIFFファイル形式のいくつかのWebページを読んだ後、あなたが話していたことを見つけました。 –

+0

申し訳ありませんが私は私の応答でとても簡潔でした。次回誰かが尋ねると、私はコードサンプルを提供します。 – BitBank

答えて

1

はここについては、ちょうどIFDチェーンを歩く方法

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() 
関連する問題