"发生共享违规行为"使用VB.NET在Paint中编辑图像时出错

时间:2014-09-28 18:14:01

标签: vb.net

我在VB.NET中编写了一个程序,用于监视从该工具中获取的最近保存的屏幕截图的文件更改。

我有一个自定义的FileSystemWatcher类(由互联网上的某人共享,只是自定义)并在我的程序中调用了一个实例。

基本问题或目标是当我使用我的程序捕获屏幕区域时,我希望它立即将其复制到剪贴板。有时我想编辑捕获的图像,在这种情况下,保存编辑后的图像后所做的任何编辑和保存都必须自动复制到剪贴板。

我使用以下代码将图像复制到剪贴板

'Subroutine to Copy the captured screenshot
'Added 15/09/2014
Sub CopyImageCapture(ByVal ImgPath)

    Dim ThreadA As Thread

    ThreadA = New Thread(AddressOf Me.MyAsyncTask)

    'Setting ApartmentState.STA as this is needed for Clipboard related functionalities
    ThreadA.SetApartmentState(ApartmentState.STA)
    ThreadA.Start(ImgPath)

End Sub

'Threading to handle the Clipboard copy function
'Copy the screenshot to Clipboard
'Added 15/09/2014
Private Sub MyAsyncTask(ByVal ImgPath)

    Clipboard.Clear()
    Clipboard.SetImage(ImgPath)

End Sub

当我选择编辑图像时,下面的代码会注意监视文件更改

Sub MonitorFileChange(ByVal FolderPath, ByVal ItemName)

    Try
        Dim sFolder As String
        Dim sFile As String
        sFolder = FolderPath
        sFile = ItemName

        If IO.Directory.Exists(sFolder) Then
            objWatcher.FolderToMonitor = sFolder
            objWatcher.FileToMonitor = sFile
            'objWatcher.NotifyFilter = (NotifyFilters.FileName Or NotifyFilters.Size)
            objWatcher.StartWatch()
        Else
            MessageBox.Show("Folder does not exist!")

        End If
    Catch ex As Exception
        MsgBox("Encountered an exception in MonitorFileChange subroutine. Details - " + ex.Message)
    End Try

End Sub

'Subrountine to capture FileChanged events (esp. when captured image is edited in Paint and Saved)
'Works in sync with custom class 'clsFSW' for this purpose.
'ADDED: 15/09/2014
Private Sub objWatcher_FileChanged(ByVal FullPath As String) Handles objWatcher.FileChanged


    'Try
    Dim lastWriteTime As DateTime
    lastWriteTime = File.GetLastWriteTime(FullPath)
    Debug.Print(lastWriteTime)

    If (lastWriteTime <> lastRead) Then
        objWatcher.EnableRaisingEvents = False

        'QuickCopy for changes files. BUG FIX
        If (QuickSaveToolStripMenuItem.Checked) Then
            Debug.Print("File Changed: " & FullPath & vbCrLf)

            Dim tempI As System.Drawing.Bitmap = New System.Drawing.Bitmap(FullPath)
            Dim tempI2 As System.Drawing.Bitmap = New System.Drawing.Bitmap(tempI, tempI.Width, tempI.Height)
            CopyImageCapture(tempI2)
            tempI.Dispose()

        End If
        lastRead = lastWriteTime
    End If
    'Catch ex As Exception
    '    MsgBox("Encountered an exception in objWatcher_FileChanged subrountine. Details - " + ex.Message)

    '    'Finally
    '    'objWatcher.EnableRaisingEvents = True
    'End Try

End Sub

问题是有时候当我打开拍摄的图像并进行快速保存或按下保存几次真正快速我得到了一个&#34; A共享违规发生&#34;错误

任何人都可以帮助解决上述问题吗?代码或逻辑的哪一部分导致了这种情况发生?

此外,如果您检查上面的代码片段,有时(没有看到任何特定的模式)但是触发objWatcher_FileChanged中的catch块并且错误是&#34;找不到参数&#34;

也有人可以帮忙吗?

提前致谢!如果需要更多信息,请告诉我。

编辑:请注意,分享违规错误似乎是从Paint应用程序本身而不是我的程序中调用的,但为什么它是随机发生的,这对我来说是一个谜。逻辑上有没有漏洞?我希望实现的任何替代逻辑?

1 个答案:

答案 0 :(得分:-1)

尝试处理tempI2。 Windows可能会锁定与tempI一起使用的文件,并保持锁定,因为它已分配给tempI2。如果是这样,它可能会保持锁定,直到tempI2被处置。