【问题标题】:" A Sharing Violation occured" error when editing images in Paint using VB.NET使用 VB.NET 在 Paint 中编辑图像时出现“发生共享冲突”错误
【发布时间】: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


    【解决方案1】:

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

    【讨论】:

    • 我在只使用一个变量 tempI 时观察到这一点,处理掉 tempI 使剪贴板为空(天知道为什么),与 tempI2 相同
    • 它可能正在将文件的引用复制到剪贴板。用clone 或类似的东西复制位图,将其分配给剪贴板,这样就可以解决问题了。
    • 谢谢,测试了它,但不幸的是它使情况恶化,因为我觉得克隆确实锁定了文件。我在这个网站上找到了这篇文章,其中提到了为什么 New Bitmap 比 Bitmap.Clone stackoverflow.com/questions/12709360/…
    • 哎呀,你是对的。 (我假设克隆复制了像素,就像我使用的几个图形库一样。)
    • @SunnyD'Souza 你是如何解决你的问题的?我也面临同样的问题,但我在通过油漆违规错误进行编辑时拍摄图像。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多