【发布时间】:2014-11-23 03:25:42
【问题描述】:
我已经在 VB.NET 中编写了一个程序来监视从该工具获取的最近保存的屏幕截图的文件更改。
我编写了一个自定义 FileSystemWatcher 类(由 Internet 上的某个人共享,刚刚自定义),并在我的程序中调用了一个实例。
基本问题或目标是,当我使用我的程序捕获屏幕区域时,我希望它立即将其复制到剪贴板。有时我想编辑捕获的图像,在这种情况下,在保存编辑后的图像后所做的任何编辑和保存都必须自动复制到剪贴板。
我使用下面的代码将图像复制到剪贴板
'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
问题是有时当我打开捕获的图像并进行快速保存或快速按保存几次时,我会收到“发生共享违规”错误
谁能帮助解决上述问题?是代码或逻辑的哪一部分导致了这种情况发生?
另外,如果你检查上面的 sn-p,有时(还没有看到任何特定的模式)但是 objWatcher_FileChanged 中的 catch 块被触发并且错误是“找不到参数”
有人也可以帮忙解决这个问题吗?
提前致谢!如果需要更多信息,请告诉我。
编辑:请注意,共享冲突错误似乎是从 Paint 应用程序本身而不是我的程序调用的,但为什么它随机发生,对我来说是个谜。逻辑上有没有漏洞?我想要实现的任何替代逻辑?
【问题讨论】:
标签: vb.net