Hello,

I have a WindowsForm in VB.NET in which I display either a single image or a slide show of images.

When I delete an image which is in a slideshow it works fine, because I first select the next image and then delete the old image.

Whenever I have a solitary image however there is a problem, because I cannot delete the image as long it is displayed by the PictureBox.

Therefore I first load another image (from My.Resources) and then Refresh(), dispose of the oldImage, set the oldImage to Nothing and still get the error

System.IO.IOException: 'The process cannot access the file 'D:\OneDrive\Afbeeldingen\Schermafbeeldingen\2016-10-03 (0) - Copy_R.png' because it is being used by another process.'

Any other suggestions?

Name:  PV_FileUsed.jpg
Views: 4129
Size:  31.0 KB

Code:
Private Sub DoDelete()
        StrOrgFile = PictureBox1.ImageLocation
        If StrOrgFile = "" Then Exit Sub

        Dim oldImage As Image = PictureBox1.Image
        Dim Response As Long = MsgBox("Are you sure you want to delete this image?", 32 + 4, "Delete?")

        If Response = vbYes Then
            BlnDelete = True
            StrStartInFolder = GetTextFromFile(SettingsPath, 0, StrErr)

            ' In case a single picture is displayed, StrStartInFolder = "",
            ' else it is in a FolderPath and another image is loaded before the oldImage is deleted
            If StrStartInFolder = "" Or StrStartInFolder = "0" Then

                'Display different image in PictureBox1
                PictureBox1.Image = My.Resources.startpage_l
                Refresh()

                'New image is displayed so I suppose I can delete the old one
                oldImage.Dispose()
                oldImage = Nothing

                System.IO.File.Delete(StrOrgFile)
                'Kill(StrOrgFile)
                'SafeFileDelete(StrOrgFile)

                'System.IO.IOException 'The process cannot access the file _
                'D:\OneDrive\Afbeeldingen\Schermafbeeldingen\2016-10-03 (0) - Copy_R.png' _
                'because it Is being used by another process.'

                Exit Sub
            Else
Grtz Bart