I've been working with the Bob Powell example for Adding pages to a multipage TIFF
and ran into some speed issues performing the add within a loop. Basically, I have a loop that is:
add to tiff
Store data in DB
I experience increasingly slow processing as I add pages to the tiff, and I understand that that is because I am re-encoding the pages again and again with each pass. I had "Parameter is Not Valid" when I collected the images into an ArrayList or List(Of Image).
When I work with the TiffBitmapEncoder I am getting "Handle is Invalid" errors with the streams for outputting the file. This attempt basically fills up a temporary directory with the scanned images, then loops through these like so:
Private Function AddPagesToMaster() As Boolean
Dim bRetVal As Boolean = False
If Directory.GetFiles(sTempFilePath).Length > 0 Then
If File.Exists(TEMP_FILE_NAME) Then File.Delete(TEMP_FILE_NAME)
Dim encoder As New System.Windows.Media.Imaging.TiffBitmapEncoder
encoder.Compression = Windows.Media.Imaging.TiffCompressOption.Ccitt4
Dim fsIn As New FileStream(TEMP_FILE_NAME, FileMode.Open, FileAccess.Read, FileShare.Read)
Dim fsOut As New FileStream(g_sMasterTif, FileMode.Create, FileAccess.ReadWrite, FileShare.None)
Dim decoder As New System.Windows.Media.Imaging.TiffBitmapDecoder(fsIn, Windows.Media.Imaging.BitmapCreateOptions.PreservePixelFormat, Windows.Media.Imaging.BitmapCacheOption.Default)
For Each frm As System.Windows.Media.Imaging.BitmapSource In decoder.Frames
Dim aImg() As String = Directory.GetFiles(sTempFilePath)
Array.Sort(aImg) 'I am naming the files so that they come back sequentially and I can figure the Page number in another proc.
For Each sPath As String In aImg
Dim strBmp As Stream = File.OpenRead(sPath)
encoder.Save(fsOut) 'Gives me a "Handle is invalid error
bRetVal = True
Catch ex As Exception
MessageBox.Show(ex.Message, "Exception in AddPagesToMaster", MessageBoxButtons.OK, MessageBoxIcon.Error)
I have tried wrapping up the fsIn and fsOut variables in "Using" statements with the same effect. I tried deleting the output file and changed the error line to
That yielded an error that I had bad metadata.
I'm using VB.net 2010 on Windows 7, 64bit. I've found a few articles that indicate changes under the hood for CCITT4 on Windows 7, but I have also changed to LZW and None for the compression to no avail. I've seen Libtiff.Net
but want to keep my number of DLLs down if I can simply code this directly. I checked MSDN
, but can't quite work out the errors described above.
Any help would be greatly appreciated.