Private Shared Values(4) As Integer
Private Shared myImage As Bitmap
Private Shared myArea As Rectangle
Private Shared ocrImg As Bitmap
Private Shared ImgCpy As Bitmap
Public Shared Function PrepImage(ByVal filename As String, ByVal savename As String, ByVal filetype As String) As Boolean
'// Grab region of image for processing
Try
Select Case filetype
Case "A"
Values(0) = 2240
Values(1) = 255
Values(2) = 40
Values(3) = 150
Values(4) = 3
Case "B"
Values(0) = 184
Values(1) = 255
Values(2) = 85
Values(3) = 20
Values(4) = 0
Case "C"
Values(0) = 933
Values(1) = 755
Values(2) = 270
Values(3) = 62
Values(4) = 0
Case Else
Return False
End Select
myImage = Bitmap.FromFile(filename)
ImgCpy = New Bitmap(myImage)
myImage.Dispose()
myArea = New Rectangle(Values(0), Values(1), Values(2), Values(3))
ocrImg = GetPicturePart(ImgCpy, myArea)
ocrImg.RotateFlip(Values(4))
ocrImg.Save(savename, Drawing.Imaging.ImageFormat.Bmp)
ocrImg.Dispose()
ImgCpy.Dispose()
这个函数在我第一次调用时工作,但随后它失败了,有时我似乎得到一个参数没有指定异常,但大多数情况下我得到一个内存不足的错误。我是否必须在最后将图像设置为Nothing,我认为Dispose将释放他们正在使用的所有资源?
我重构了如下代码,但仍有问题,这不使用get picturepart函数,但在这里它也是......
Private Shared Function GetPicturePart(ByVal SourceImage As Image, ByVal Region As Rectangle) As Bitmap
Dim ImagePart As Bitmap = New Bitmap(Region.Width, Region.Height)
Using G As Graphics = Graphics.FromImage(ImagePart)
Dim TargetRect As Rectangle = New Rectangle(0, 0, Region.Width, Region.Height)
Dim SourceRect As Rectangle = Region
G.DrawImage(SourceImage, TargetRect, SourceRect, GraphicsUnit.Pixel)
End Using
Return ImagePart
End Function
Dim myImg As New Bitmap(filename)
Dim MyArea As New Rectangle(Values(0), Values(1), Values(2), Values(3))
Dim myFormat As PixelFormat = myImg.PixelFormat
Dim myOCR As Bitmap = myImg.Clone(MyArea, myFormat)
myImg.Dispose()
myImg = Nothing
myOCR.RotateFlip(Values(4))
myOCR.Save(savename, Drawing.Imaging.ImageFormat.Bmp)
myOCR.Dispose()
myOCR = Nothing
答案 0 :(得分:0)
感谢所有回复的人。以下是我能够确定的内容:
更改代码似乎是我发布的第二个似乎消除了内存不足错误。只需减少代码中的步骤,并在使用后立即处理图像并将其设置为空。
参数是不正确的问题,而模糊实际上是因为其他东西对文件有锁定所以进程能够加载它。我最好的猜测,因为我是从测试文件夹复制并粘贴到生产捕获文件夹中,系统会更快地抓取文件,然后系统就能完成操作,或者可能因为Windows尝试生成拇指,它试图做它,我不确定。
在任何一种情况下,我都会在尝试图像操作之前将线程休眠几秒钟,但我无法再现任何错误......