0

I have written a program in VB.NET to monitor file changes to recent saved screenshots taken from the tool.

I have a custom FileSystemWatcher class written (shared by someone on the internet,just customized) and have an instance invoked in my program.

The basic issue or objective is that when i capture a screen area using my program, I want it to immediately copy it over to clipboard. There are times when I would want to edit the captured image, in which case any editing made and saved, after saving the edited image must automatically be copied over to clipboard.

I use the below code to copy the image over to clipboard

'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

And when i choose to edit the image, the below code takes care to monitor the file changes

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

The problem is sometimes when I open the captured image and make quick saves or press save a couple of times real quick I get a "A Sharing violation occured" error

Can anyone help resolve the above issue? Which part of the code or logic is causing this to happen?

Also, if you check the above snippet, sometimes (havent seen any particular pattern) but the catch block in objWatcher_FileChanged is triggered and the error is "Parameter not found"

Can someone please help into this too?

Thanks in advance! Please let me know if any info more is required.

EDIT: Please note, the sharing violation error seems invoked from the Paint application itself and not my program, but why does it happen randomly, is a mystery to me. Is there nay loophole in the logic? Any alternative logic to what I want to achieve?

Sunny D'Souza
  • 616
  • 5
  • 11
  • 27

1 Answers1

-1

Try disposing tempI2. Windows may be locking the file used with tempI, and keeping it locked since it is assigned to tempI2. If so, it may remain locked until tempI2 is disposed.

xpda
  • 15,585
  • 8
  • 51
  • 82
  • I observed this when I used just one variable tempI, disposing off tempI made the clipboard empty (lord knows why), same with tempI2 – Sunny D'Souza Sep 28 '14 at 18:43
  • It may be copying a reference of the file to the clipboard. Duplicate the bitmap with `clone` or something equivalent, assign that one to the clipboard, and that might take care of it. – xpda Sep 28 '14 at 18:46
  • Thanks, tested it but unfortunately it worsens the situation as I feel Clone literally locks the file. I found this article on this site, which mentioned why New Bitmap is much better than Bitmap.Clone http://stackoverflow.com/questions/12709360/whats-the-difference-between-bitmap-clone-and-new-bitmapbitmap – Sunny D'Souza Sep 29 '14 at 04:19
  • Oops, you're correct. (I assumed that clone made a copy of the pixels like a couple of graphics libraries I use.) – xpda Sep 29 '14 at 04:31
  • @SunnyD'Souza how did you solve your issue? i am facing the same issue, but i take image from and while editing through paint violation error occurs. –  Apr 13 '15 at 05:54