2014-09-28 63 views
0

我已经在VB.NET中编写了一个程序,用于监视从该工具获取的最近保存的屏幕截图的文件更改。使用VB.NET在Paint中编辑图像时发生“共享冲突”错误

我有一个自定义的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 

的问题是有时当我打开捕获的图像并快速保存或按保存几次真正快速我得到了“共享冲突发生”错误

Can an an yone帮助解决上述问题?哪部分代码或逻辑导致了这种情况发生?

此外,如果您检查上面的代码中,有时(还没有看到任何具体的模式),但在objWatcher_FileChanged catch块被触发,错误是“参数未找到”

能有人帮到这个吗?

在此先感谢!如果需要更多信息,请让我知道。

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

回答

-1

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

+0

当我仅使用一个变量tempI时,观察到了这一点,将tempI设置为空使剪贴板为空(Lord知道为什么),与tempI2相同 – 2014-09-28 18:43:03

+0

它可能会将该文件的引用复制到剪贴板。用'clone'或类似的东西复制位图,将其分配给剪贴板,这可能会处理它。 – xpda 2014-09-28 18:46:47

+0

谢谢,测试了它,但不幸的是,它让我感觉克隆会逐渐锁定文件,从而使情况更加恶化。我在这个网站上发现了这篇文章,其中提到了为什么新的位图比Bitmap.Clone好得多http://stackoverflow.com/questions/12709360/whats-the-difference-between-bitmap-clone-and-new-bitmapbitmap – 2014-09-29 04:19:48

相关问题