Avatar of gfahd1
gfahd1 asked on

Error loading TIFF Image into Bitmap object

Hello,
This problem driving crazy. I am loading tiff images into a bitmap object and passing the object to LuraTech. All working well, except for some tiff files (large files, not compressed, color, 650MB). Whe I get to pass this file to the bitmap object, i get an error: "Parameter is not valid." and Inner error: "Referenced object has a value of 'Nothing'.". The strange thing is, it does work on some machines and not on others. I couldn't find any pattern for the problem except the dot net framework release. The one that works is: 2.00.50727.0042 and my machine doesn't work with the version: 2.00.50727.1433.

Any help is appreciated. Thanks in advanced.
Private Function LoadBitmap(ByVal filename As String) As Bitmap
        Dim bitmap As Bitmap = Nothing
 
        Try
            ' load bitmap
            bitmap = New Bitmap(filename)     'ERROR HERE
        Catch ex As Exception
            moLog.Write("Could not load image to Bitmap. Unsupported file format.", moMail, True)
            moLog.Write(ex.ToString.ToString, moMail, True)
            Return Nothing
 
        End Try

Open in new window

.NET ProgrammingVisual Basic.NET

Avatar of undefined
Last Comment
gfahd1

8/22/2022 - Mon
Bob Learned

Are these multi-page TIFF or single page?  It sounds like, on the surface, that it is a bug with 2005 SP1.
ASKER
gfahd1

these are multi-page tiff (96 pages). I tried 2008 and no luck. Works fine on some computers. Does not work at the client PC nor at my pc (developer). very strange.driving me crazy. Thanks
Bob Learned

Are you trying to pull the frames fromthe multi-page TIFF?
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER
gfahd1

No, I am just trying to LuraTech for  load it and pass it to another program LuraTech to compress it. I just found out that if I compile the program, it works. But does not work if I step through it in vb.net 2008.very strange. Thanks
Bob Learned

What is the operating system(s)?
ASKER
gfahd1

I am using Windows XP Pro SP2
Installed visual studio 2005 and 2008
Framework 1, 2 and 3

Thanks for your help
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Bob Learned

If you are running the application with 2.0 framework, did you install SP1?
ASKER
gfahd1

I did not install SP1. let me try it now...
ASKER
gfahd1

Still No luck. removed all framework versions (with the cleanup utility), reinstalled 2.0 and then sp1, still same problem "parameter is not valid" and an inner exception of " Referenced object has a value of 'nothing'".

The strange problem is at the client side, they process few files, and then it starts failing with the above error.There is no pattern and there is no specific file. The failed files will work after stopping and restarting the service (that processes these files and compress them.). As for my end, the bitmap object either always failing a a specific file, or always working. At our test machines, it is always working on all files, even on the client files, never fails. So, I am going crazy trying to figure out what is going on. Any suggestions are appreciated. Thanks
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER
gfahd1

here is a the complete code that I am using to process images, You might see something I could change, at least for the cl;ient, so the bitmap object does not fail after few files.
Try
                    loBitmap = New Bitmap(lsFilePath)
 
                Catch ex As Exception
                    moLog.Write("Could not load image to Bitmap. Unsupported file format.", moMail, True)
                    moLog.Write(ex.ToString.ToString, moMail, True)
                    loBitmap = Nothing
 
                End Try
 
                If IsNothing(loBitmap) Then
                    moLog.Write("Failed loading. Move to Failed folder and Skip ...", moMail, False, mbDebugMode)
                    'Error. Move file to Error directory. and go to next file.
                    'Does error directory exist? if not create it.
                    If Not My.Computer.FileSystem.DirectoryExists(moCompression.SourceFolder & "Error") Then
                        My.Computer.FileSystem.CreateDirectory(moCompression.SourceFolder & "Error")
                    End If
                    'Move file
                    My.Computer.FileSystem.MoveFile(lsFilePath, moCompression.SourceFolder & "Error\" & lsFileName, True)
                    GoTo skip
                End If
 
                moLog.Write("Create folder: " & lsFolderName, moMail, False, mbDebugMode)
                My.Computer.FileSystem.CreateDirectory(moCompression.TempDestFolder1 & lsFolderName)
                moLog.Write("Folder created successfully.", moMail, False, mbDebugMode)
 
                Dim objGuid As Guid = loBitmap.FrameDimensionsList(0)
                Dim objDimension As System.Drawing.Imaging.FrameDimension = New System.Drawing.Imaging.FrameDimension(objGuid)
                Dim liCount As Integer = loBitmap.GetFrameCount(objDimension)
 
                Dim lsDestFilePath As String = ""
                moLog.Write("Image Counts to process: " & liCount, moMail, False, mbDebugMode)
                For liCounter As Integer = 1 To liCount
                    loBitmap.SelectActiveFrame(objDimension, liCounter - 1)
 
                    'Check if we need to rotate the image
                    lbRotate = (loBitmap.Width > loBitmap.Height)
                    moLog.Write("Should we rotate? lbRotate= " & lbRotate, moMail, False, mbDebugMode)
 
                    lsDestFilePath = moCompression.TempDestFolder1 & lsFolderName & "\" & lsFolderName & "_" & Format(liCounter, "000000") & "." & moCompression.CompressionType1
                    moLog.Write("lsDestFilePath: " & lsDestFilePath, moMail, False, mbDebugMode)
 
                    moLog.Write("Compress image: " & liCounter, moMail, False, mbDebugMode)
                    If Not Compress(moCompressor1, loBitmap, lsDestFilePath, lbRotate) Then
                        Throw New Exception("Error on encoding image: " & liCounter)
                    End If
                    moLog.Write("Compression successful.", moMail, False, mbDebugMode)
 
                Next liCounter
 
                'If destination folder exists, delete it.
                If My.Computer.FileSystem.DirectoryExists(moCompression.DestFolder1 & lsFolderName) Then
                    My.Computer.FileSystem.DeleteDirectory(moCompression.DestFolder1 & lsFolderName, FileIO.DeleteDirectoryOption.DeleteAllContents)
                End If
 
                'Move folder to final destination
                moLog.Write("Move: " & moCompression.TempDestFolder1 & " to: " & moCompression.DestFolder1 & lsFolderName, moMail, False, mbDebugMode)
                My.Computer.FileSystem.MoveDirectory(moCompression.TempDestFolder1 & lsFolderName, moCompression.DestFolder1 & lsFolderName, True)
                moLog.Write("Moving successful.", moMail, False, mbDebugMode)
 
'Clean up
                objGuid = Nothing
                objDimension = Nothing
                If Not loBitmap Is Nothing Then
                    loBitmap.Dispose()
                    loBitmap = Nothing
                End If

Open in new window

ASKER
gfahd1

Sorry, here is the complete code. The one above is missing some info
 Public Sub DeliverWithComp()
        Dim lsRemoteFile As String = ""
        Dim lsFileName As String = ""
        Dim lsFilePath As String = ""
        Dim lbRelaisRequest As Boolean = False
        Dim lsFolderName As String = ""
        Dim loBitmap As Drawing.Bitmap = Nothing
        Dim lbRotate As Boolean = False
 
        Try
 
            For i As Integer = moFileList.Count - 1 To 0 Step -1
                lsFileName = moFileList.Item(i).Substring(moFileList.Item(i).LastIndexOf("\") + 1)
                moLog.Write("lsFileName:" & lsFileName, moMail, False, mbDebugMode)
 
                lsFilePath = moFileList.Item(i)
                moLog.Write("lsFilePath:" & lsFilePath, moMail, False, mbDebugMode)
 
                'create request folder if doesn't exists.
                lsFolderName = lsFileName.Substring(0, lsFileName.LastIndexOf("."))
                moLog.Write("lsFolderName: " & lsFolderName, moMail, False, mbDebugMode)
 
                moLog.Write("Check if folder exists: " & lsFolderName, moMail, False, mbDebugMode)
                If My.Computer.FileSystem.DirectoryExists(moCompression.TempDestFolder1 & lsFolderName) Then
                    moLog.Write("It exists, delete it and recreate it.", moMail, False, mbDebugMode)
                    My.Computer.FileSystem.DeleteDirectory(moCompression.TempDestFolder1 & lsFolderName, FileIO.DeleteDirectoryOption.DeleteAllContents)
                    moLog.Write("Successfully deleted.", moMail, False, mbDebugMode)
                End If
 
                ' Try to load the file into a Bitmap
                moLog.Write("Load image into bitmap:" & lsFilePath, moMail, False, mbDebugMode)
 
                Try
                    loBitmap = New Bitmap(lsFilePath)
 
                Catch ex As Exception
                    moLog.Write("Could not load image to Bitmap. Unsupported file format.", moMail, True)
                    moLog.Write(ex.ToString.ToString, moMail, True)
                    loBitmap = Nothing
 
                End Try
 
                If IsNothing(loBitmap) Then
                    moLog.Write("Failed loading. Move to Failed folder and Skip ...", moMail, False, mbDebugMode)
                    'Error. Move file to Error directory. and go to next file.
                    'Does error directory exist? if not create it.
                    If Not My.Computer.FileSystem.DirectoryExists(moCompression.SourceFolder & "Error") Then
                        My.Computer.FileSystem.CreateDirectory(moCompression.SourceFolder & "Error")
                    End If
                    'Move file
                    My.Computer.FileSystem.MoveFile(lsFilePath, moCompression.SourceFolder & "Error\" & lsFileName, True)
                    GoTo skip
                End If
 
                moLog.Write("Create folder: " & lsFolderName, moMail, False, mbDebugMode)
                My.Computer.FileSystem.CreateDirectory(moCompression.TempDestFolder1 & lsFolderName)
                moLog.Write("Folder created successfully.", moMail, False, mbDebugMode)
 
                Dim objGuid As Guid = loBitmap.FrameDimensionsList(0)
                Dim objDimension As System.Drawing.Imaging.FrameDimension = New System.Drawing.Imaging.FrameDimension(objGuid)
                Dim liCount As Integer = loBitmap.GetFrameCount(objDimension)
 
                Dim lsDestFilePath As String = ""
                moLog.Write("Image Counts to process: " & liCount, moMail, False, mbDebugMode)
                For liCounter As Integer = 1 To liCount
                    loBitmap.SelectActiveFrame(objDimension, liCounter - 1)
 
                    'Check if we need to rotate the image
                    lbRotate = (loBitmap.Width > loBitmap.Height)
                    moLog.Write("Should we rotate? lbRotate= " & lbRotate, moMail, False, mbDebugMode)
 
                    lsDestFilePath = moCompression.TempDestFolder1 & lsFolderName & "\" & lsFolderName & "_" & Format(liCounter, "000000") & "." & moCompression.CompressionType1
                    moLog.Write("lsDestFilePath: " & lsDestFilePath, moMail, False, mbDebugMode)
 
                    moLog.Write("Compress image: " & liCounter, moMail, False, mbDebugMode)
                    If Not Compress(moCompressor1, loBitmap, lsDestFilePath, lbRotate) Then
                        Throw New Exception("Error on encoding image: " & liCounter)
                    End If
                    moLog.Write("Compression successful.", moMail, False, mbDebugMode)
 
                Next liCounter
 
                'If destination folder exists, delete it.
                If My.Computer.FileSystem.DirectoryExists(moCompression.DestFolder1 & lsFolderName) Then
                    My.Computer.FileSystem.DeleteDirectory(moCompression.DestFolder1 & lsFolderName, FileIO.DeleteDirectoryOption.DeleteAllContents)
                End If
 
                'Move folder to final destination
                moLog.Write("Move: " & moCompression.TempDestFolder1 & " to: " & moCompression.DestFolder1 & lsFolderName, moMail, False, mbDebugMode)
                My.Computer.FileSystem.MoveDirectory(moCompression.TempDestFolder1 & lsFolderName, moCompression.DestFolder1 & lsFolderName, True)
                moLog.Write("Moving successful.", moMail, False, mbDebugMode)
 
                moLog.Write("Is compressor2 activated: " & mbCompressor2, moMail, False, mbDebugMode)
                If mbCompressor2 Then
                    moLog.Write("Check if folder exists: " & lsFolderName, moMail, False, mbDebugMode)
                    If My.Computer.FileSystem.DirectoryExists(moCompression.TempDestFolder2 & lsFolderName) Then
                        moLog.Write("It exists, delete it and recreate it.", moMail, False, mbDebugMode)
                        My.Computer.FileSystem.DeleteDirectory(moCompression.TempDestFolder2 & lsFolderName, FileIO.DeleteDirectoryOption.DeleteAllContents)
                        moLog.Write("Successfully deleted.", moMail, False, mbDebugMode)
                    End If
                    moLog.Write("Create folder: " & lsFolderName, moMail, False, mbDebugMode)
                    My.Computer.FileSystem.CreateDirectory(moCompression.TempDestFolder2 & lsFolderName)
                    moLog.Write("Folder created successfully.", moMail, False, mbDebugMode)
 
                    For liCounter As Integer = 1 To liCount
 
                        loBitmap.SelectActiveFrame(objDimension, liCounter - 1)
 
                        'Check if we need to rotate the image
                        lbRotate = (loBitmap.Width > loBitmap.Height)
                        moLog.Write("Should we rotate? lbRotate= " & lbRotate, moMail, False, mbDebugMode)
 
                        lsDestFilePath = moCompression.TempDestFolder2 & lsFolderName & "\" & lsFolderName & "_" & Format(liCounter, "000000") & "." & moCompression.CompressionType2
                        moLog.Write("lsDestFilePath: " & lsDestFilePath, moMail, False, mbDebugMode)
 
                        moLog.Write("Compress file: " & liCount, moMail, False, mbDebugMode)
                        If Not Compress(moCompressor2, loBitmap, lsDestFilePath, lbRotate) Then
                            Throw New Exception("Error on encoding image: " & liCount)
                        End If
                        moLog.Write("Compression successfull.", moMail, False, mbDebugMode)
 
                    Next liCounter
 
                    'Move folder to final destination
                    moLog.Write("Move: " & moCompression.TempDestFolder2 & " to: " & moCompression.DestFolder2 & lsFolderName, moMail, False, mbDebugMode)
                    My.Computer.FileSystem.MoveDirectory(moCompression.TempDestFolder2 & lsFolderName, moCompression.DestFolder2 & lsFolderName, True)
                    moLog.Write("Moving successful.", moMail, False, mbDebugMode)
                End If
 
                'Clean up
                objGuid = Nothing
                objDimension = Nothing
                If Not loBitmap Is Nothing Then
                    loBitmap.Dispose()
                    loBitmap = Nothing
                End If
 
                moLog.Write("Validating File format to find out if it is a Relais request...", moMail, False, mbDebugMode)
                lbRelaisRequest = ValidateFormat(lsFileName)
 
                moLog.Write("mbUpdateDatabase= " & mbUpdateDatabase, moMail, False, mbDebugMode)
                If mbUpdateDatabase And lbRelaisRequest Then
                    moLog.Write("Updating database...", moMail, False, mbDebugMode)
                    If Not UpdateDatabase(lsFileName) Then
                        Throw New Exception("Error updating database.")
                    End If
                    moLog.Write("Database update successfull.", moMail, False, mbDebugMode)
                End If
 
                'Remove file from list and delete tiff from source dir
                moLog.Write("deleting file from processing directory...", moMail, False, mbDebugMode)
                My.Computer.FileSystem.DeleteFile(lsFilePath)
                moLog.Write("Deletion successfull.", moMail, False, mbDebugMode)
 
                moLog.Write("Remove file from list...", moMail, False, mbDebugMode)
                moFileList.Remove(moFileList.Item(i))
                moLog.Write("Next file if exists...", moMail, False, mbDebugMode)
Skip:
            Next i
 
        Catch ex As Exception
 
            If Not loBitmap Is Nothing Then
                loBitmap.Dispose()
                loBitmap = Nothing
            End If
 
            Throw New Exception(ex.ToString)
            
        Finally
            moFileList.Clear()
 
        End Try
 
    End Sub

Open in new window

Bob Learned

What line do you get 'Parameter is invalid' on?  What line do you get 'Referenced object is nothing' on?
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
gfahd1

Line 34: loBitmap = New Bitmap(lsFilePath)
It has to do with memory, we are able to generate this problem on another test machine here. After shutting down the service and restarting, it processes fine the previously errored tiff files. Thanks
ASKER CERTIFIED SOLUTION
Bob Learned

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
gfahd1

Hello,
I changed my code and optimized it as TheLearnedOne had, but still no luck. app crushing after few files. But if i restart the service, it works fine. so I created another app to restart the service everytime it crushes. Working fine for now. I will reward the point to TheLearnedOne. thanks for your hel.