经我方多次测试,锁定为该方法导致的内存增加:
TmpBarcode = tw.DoReadBarcodes(PicPath, Me)
该方法调用了你们的DoReadBarcodes函数。我们对这个函数进行了一定变更,但相关的dispose并没有动,改动后如下:
Public Function DoReadBarcodes(ByVal filePath As String, ByVal owner As IWin32Window) As String
Dim returnBarcode As String = ""
Dim returnBarcodeFace As String = "" '面卡拼接的字符串
'stream = System.IO.File.Open("E:\LeadTools\a.txt", FileMode.Append)
'Dim documentFileNames As String() = New String() {Path.Combine("C:\Users\Public\Documents\LEADTOOLS Images\", "Barcode1.tif")}
'Dim documentFileNames As String() = New String() {filePath}
'Dim documentFileName As String = filePath
Dim documentFileName As String = System.IO.Path.GetDirectoryName(filePath) & "\" & System.IO.Path.GetFileNameWithoutExtension(filePath) & "-tmp.tif"
Dim barcodes As BarcodeData()
Dim starttime As Date = Now()
Dim endtime As Date
Dim pageImage As RasterImage = Nothing
Dim _barcodeEngine As BarcodeEngine = New BarcodeEngine()
Dim _rasterCodecs As RasterCodecs = New RasterCodecs()
Dim command As AutoBinarizeCommand = New AutoBinarizeCommand()
pageImage = _rasterCodecs.Load(filePath)
command.Run(pageImage)
_rasterCodecs.Save(pageImage, filePath, Leadtools.RasterImageFormat.Tif, 1, pageImage.Page, pageImage.PageCount, 1, Leadtools.Codecs.CodecsSavePageMode.Overwrite)
pageImage.Dispose()
If Me.getGrayImage(filePath, documentFileName) Then
''Dim image As RasterImage = Nothing
'Dim pageImage As RasterImage = Nothing
'Dim _barcodeEngine As BarcodeEngine = New BarcodeEngine()
'Dim _rasterCodecs As RasterCodecs = New RasterCodecs()
''For Each documentFileName As String In documentFileNames
'Dim command As AutoBinarizeCommand = New AutoBinarizeCommand()
If File.Exists(documentFileName) Then
Try
Using wait As WaitCursor = New WaitCursor()
pageImage = _rasterCodecs.Load(documentFileName)
'图片增强
'command.Run(pageImage)
'_rasterCodecs.Save(pageImage, "E:\2016-1-12海航扫描图片\临时加强文件\" + Path.GetFileName(filePath), Leadtools.RasterImageFormat.Tif, 1, pageImage.Page, pageImage.PageCount, 1, Leadtools.Codecs.CodecsSavePageMode.Overwrite)
'If image Is Nothing Then
' image = pageImage
'Else
' image.AddPage(pageImage)
' pageImage.Dispose()
'End If
End Using
Catch ex As Exception
Messager.ShowFileOpenError(owner, documentFileName, ex)
End Try
End If
'Next documentFileName
Dim [error] As Exception = Nothing
Dim ImgWidth As Integer
Dim ImgHeight As Integer
If pageImage Is Nothing Then
Return returnBarcode
Else
ImgWidth = pageImage.Width
ImgHeight = pageImage.Height
End If
' Continue on errors
'_barcodeEngine.Reader.ErrorMode = BarcodeReaderErrorMode.IgnoreAll
'AddHandler _barcodeEngine.Reader.ReadSymbology, AddressOf Reader_ReadSymbology
'面卡,返回条码&FAACE
'普通例行工卡和非单,返回条码
Dim countExp As Integer = 0 '记录读取出错次数
'Dim countDoBarcode As Integer = 0 '记录读取次数
Dim regAz As Regex = New Regex("^[A-Za-z]+$")
dotry:
Try
endtime = Now()
Debug.Print("doreadbarcodes before ReadBarcodes :" & (endtime - starttime).TotalMilliseconds)
starttime = Now()
_barcodeEngine.Reader.ErrorMode = BarcodeReaderErrorMode.IgnoreAll
barcodes = _barcodeEngine.Reader.ReadBarcodes(pageImage, LogicalRectangle.FromLTRB(0, 0, ImgWidth, ImgHeight, LogicalUnit.Pixel), 0, BarcodeEngine.GetSupportedSymbologies(), Nothing)
endtime = Now()
Debug.Print("doreadbarcodes ReadBarcodes:" & (endtime - starttime).TotalMilliseconds)
starttime = Now()
'countDoBarcode += 1
Catch ex As Exception
[error] = ex
countExp += 1
'Application.DoEvents()
Debug.Print("ReadBarcodes: " & ex.Message & vbCrLf & ex.StackTrace)
If countExp > 3 Then
'Messager.ShowInformation(owner, documentFileName & "读取条码出错")
barcodes = Nothing
GC.Collect()
Exit Try
End If
GC.Collect()
Debug.Print("读取条码出错:" & documentFileName & vbCrLf & ex.Message & vbCrLf & ex.StackTrace)
GoTo dotry
'Return returnBarcode
End Try
regAz = Nothing
pageImage.Dispose()
_barcodeEngine = Nothing
_rasterCodecs = Nothing
command = Nothing
Else
barcodes = Nothing
End If
'Dim pageBarcodes As PageBarcodes = New PageBarcodes()
If barcodes IsNot Nothing Then
For Each barcode As BarcodeData In barcodes
'pageBarcodes.Barcodes.Add(barcode)
If barcode.Value.Contains("AA-") Then
returnBarcode = barcode.Value
'If returnBarcode.Length >= 15 AndAlso regAz.IsMatch(returnBarcode.Substring(3, 12)) AndAlso countDoBarcode < 5 Then '条形码中有字母,返回读取不到5次
' GoTo dotry
'End If
ElseIf barcode.Value = "FACE" Then
returnBarcodeFace = "FACE"
ElseIf barcode.Value.Contains("BA-") Then
returnBarcode = barcode.Value
'If returnBarcode.Length >= 15 AndAlso regAz.IsMatch(returnBarcode.Substring(3, 12)) AndAlso countDoBarcode < 5 Then '条形码中有字母,返回读取不到5次
' GoTo dotry
'End If
Exit For
End If
Next barcode
returnBarcode = returnBarcode & returnBarcodeFace
End If
If System.IO.File.Exists(documentFileName) Then
System.IO.File.Delete(documentFileName)
End If
GC.Collect()
endtime = Now()
Debug.Print("doreadbarcodes end:" & (endtime - starttime).TotalMilliseconds)
Return returnBarcode
End Function |