Calling NumberOfTiffImages from .NET

  Imaging Toolkit

Issue:
I´m trying to determine the number of pages inside a multipage-tiff which is stored inside a database. As far as I know, the only way to do this without creating a temporary file on disk is to call the method “NumberOfTiffImages” from the BiTiff.dll, via DllImport. I tried to make this work by modifying the “Load Tiff from Memory – C#” example and calling “NumberOfTiffImages” after the tiff is successfully loaded. Sadly all that gives me is an AccessViolationException “Attempted to read or write protected memory”

Solution:

LoadTiffFromMemory loads the TIFF file into a memory DIB. This is different from a TIFF chain handle, what the NumberOfTiffImages function expects. This is why it crashes.
If you wish to use this function, you need to import all functions from the DLL that you’re using because the ActiveX control doesn’t support these functions.1. Use the OpenTiffInMemory to open the TIFF file. This function returns a TIFF chain handle.
http://www.blackice.com/Help/Tools/Docu … Memory.htm
Please note that this function expects the TIFF file data in a memory buffer which can be created using the GlobalAlloc system function which has to be destroyed using GlobalFree.
http://www.pinvoke.net/default.aspx/ker … Alloc.html
http://www.pinvoke.net/default.aspx/ker … lFree.html2. Pass the TIFF chain handle to the NumberOfTiffImages function.
http://www.blackice.com/Help/Tools/Docu … Images.htm

3. Close the TIFF file using CloseTiffFile after processing to avoid any memory leaks as these DLL functions are unmanaged.
http://www.blackice.com/Help/Tools/Docu … ffFile.htm

Regards,
Technical Support Team