Object is currently in use elsewhere
Posted: Wed Dec 22, 2010 12:30 am
This unhandled exception in the Imaging SDK code is a little frustrating, as it comes up randomly, and crashes my application. Like many people, I have a Thumbnail Viewer and an Image Viewer on my Winforms form. All of my images are in TIFF format. A context menu allows the user to "cleanup" the page (deskew, despeckle, sharpen). This works okay much of the time, but occasionally the multi-threaded SDK code finds that an image is locked (don't know why, it just does) and because an exception happens on a secondary thread, it kills the whole app. I don't care if the requested command fails, I just need it to not crash my program! Is there anything I'm doing to cause this?
BTW - sharpen and despeck work fine, deskew doesn't seem to do much.
Here is all my Vinta-related code (VB.Net). I'm not bothering to do the async stuff, just working on the current image in place.
cmi = context menu item, this is for Fix / Cleanup
''' <summary>
''' Cleans up the currently focused image.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub cmiFix_Cleanup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmiFix_Cleanup.Click
cleanup(TiffViewer.FocusedImage)
End Sub
''' <summary>
''' Fix Rotate the currently focused image.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub cmiFix_Rotate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmiFix_Rotate.Click
rotate(TiffViewer.FocusedImage)
End Sub
''' <summary>
''' Cleans up the currently focused image.
''' </summary>
''' <remarks></remarks>
Private Sub cleanup(ByVal img As VintasoftImage)
despeckle(img)
deskew(img)
sharpen(img)
End Sub
''' <summary>
''' Change the format to RGB 24, necessary for sharpening.
''' </summary>
''' <param name="img"></param>
''' <remarks></remarks>
Private Sub convertToRGB24(ByVal img As VintasoftImage)
Try
Dim cmd As New ChangePixelFormatCommand(PixelFormat.Bgr24)
cmd.ExecuteInPlace(img)
Catch
End Try
End Sub
''' <summary>
''' Deskews the image.
''' </summary>
''' <param name="img"></param>
''' <remarks></remarks>
Private Sub deskew(ByVal img As VintasoftImage)
Try
Dim cmd As New Document.DeskewCommand()
cmd.ExecuteInPlace(img)
Catch
End Try
End Sub
''' <summary>
''' Remove random specks from the image.
''' </summary>
''' <param name="img"></param>
''' <remarks></remarks>
Private Sub despeckle(ByVal img As VintasoftImage)
Try
Dim level1 As Integer = 8
Dim level2 As Integer = 25
Dim radius As Integer = 30
Dim level3 As Integer = 400
' increase standard values for very big and big images
If (img.Width * img.Height) > 100000000 Then
' 100 MPixels
level1 *= 4
level2 *= 4
radius *= 4
level3 *= 4
ElseIf (img.Width * img.Height) > 60000000 Then
' 60 MPixels
level1 *= 2
level2 *= 2
radius *= 2
level3 *= 2
End If
Dim cmd As New Document.DespeckleCommand(level1, level2, radius, level3)
cmd.ExecuteInPlace(img)
Catch
End Try
End Sub
' Checks that focused image is present and correct.
Private Function isValidImage() As Boolean
Try
isValidImage = Not (TiffViewer.FocusedIndex = -1 OrElse TiffViewer.Images.Count = 0 OrElse TiffViewer.FocusedImage.IsBad)
Catch
isValidImage = False
End Try
End Function
''' <summary>
''' Rotates the image 180 degrees.
''' </summary>
''' <param name="img"></param>
''' <remarks></remarks>
Private Sub rotate(ByVal img As VintasoftImage)
Try
Dim cmd As New Transforms.RotateCommand(180)
cmd.ExecuteInPlace(img)
Catch
End Try
End Sub
''' <summary>
''' Sharpens the image.
''' </summary>
''' <param name="img"></param>
''' <remarks></remarks>
Private Sub sharpen(ByVal img As VintasoftImage)
Try
Select Case img.PixelFormat
Case PixelFormat.Bgr24, PixelFormat.Bgr32, PixelFormat.Bgra32, PixelFormat.Bgr48, PixelFormat.Bgra64
' format is already okay
Case Else
convertToRGB24(img)
End Select
Dim cmd As New Filters.SharpenCommand()
cmd.ExecuteInPlace(img)
Catch
End Try
End Sub
BTW - sharpen and despeck work fine, deskew doesn't seem to do much.
Here is all my Vinta-related code (VB.Net). I'm not bothering to do the async stuff, just working on the current image in place.
cmi = context menu item, this is for Fix / Cleanup
''' <summary>
''' Cleans up the currently focused image.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub cmiFix_Cleanup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmiFix_Cleanup.Click
cleanup(TiffViewer.FocusedImage)
End Sub
''' <summary>
''' Fix Rotate the currently focused image.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub cmiFix_Rotate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmiFix_Rotate.Click
rotate(TiffViewer.FocusedImage)
End Sub
''' <summary>
''' Cleans up the currently focused image.
''' </summary>
''' <remarks></remarks>
Private Sub cleanup(ByVal img As VintasoftImage)
despeckle(img)
deskew(img)
sharpen(img)
End Sub
''' <summary>
''' Change the format to RGB 24, necessary for sharpening.
''' </summary>
''' <param name="img"></param>
''' <remarks></remarks>
Private Sub convertToRGB24(ByVal img As VintasoftImage)
Try
Dim cmd As New ChangePixelFormatCommand(PixelFormat.Bgr24)
cmd.ExecuteInPlace(img)
Catch
End Try
End Sub
''' <summary>
''' Deskews the image.
''' </summary>
''' <param name="img"></param>
''' <remarks></remarks>
Private Sub deskew(ByVal img As VintasoftImage)
Try
Dim cmd As New Document.DeskewCommand()
cmd.ExecuteInPlace(img)
Catch
End Try
End Sub
''' <summary>
''' Remove random specks from the image.
''' </summary>
''' <param name="img"></param>
''' <remarks></remarks>
Private Sub despeckle(ByVal img As VintasoftImage)
Try
Dim level1 As Integer = 8
Dim level2 As Integer = 25
Dim radius As Integer = 30
Dim level3 As Integer = 400
' increase standard values for very big and big images
If (img.Width * img.Height) > 100000000 Then
' 100 MPixels
level1 *= 4
level2 *= 4
radius *= 4
level3 *= 4
ElseIf (img.Width * img.Height) > 60000000 Then
' 60 MPixels
level1 *= 2
level2 *= 2
radius *= 2
level3 *= 2
End If
Dim cmd As New Document.DespeckleCommand(level1, level2, radius, level3)
cmd.ExecuteInPlace(img)
Catch
End Try
End Sub
' Checks that focused image is present and correct.
Private Function isValidImage() As Boolean
Try
isValidImage = Not (TiffViewer.FocusedIndex = -1 OrElse TiffViewer.Images.Count = 0 OrElse TiffViewer.FocusedImage.IsBad)
Catch
isValidImage = False
End Try
End Function
''' <summary>
''' Rotates the image 180 degrees.
''' </summary>
''' <param name="img"></param>
''' <remarks></remarks>
Private Sub rotate(ByVal img As VintasoftImage)
Try
Dim cmd As New Transforms.RotateCommand(180)
cmd.ExecuteInPlace(img)
Catch
End Try
End Sub
''' <summary>
''' Sharpens the image.
''' </summary>
''' <param name="img"></param>
''' <remarks></remarks>
Private Sub sharpen(ByVal img As VintasoftImage)
Try
Select Case img.PixelFormat
Case PixelFormat.Bgr24, PixelFormat.Bgr32, PixelFormat.Bgra32, PixelFormat.Bgr48, PixelFormat.Bgra64
' format is already okay
Case Else
convertToRGB24(img)
End Select
Dim cmd As New Filters.SharpenCommand()
cmd.ExecuteInPlace(img)
Catch
End Try
End Sub