samedi 11 avril 2015

How to release picture from picturebox so picture file may be deleted in VB.NET

I have a picture box ThePic on the form PicViewer that is assigned an image using the below code from an array of possible picture names (FileNameSt) stored in PicList where CurNum is the array position of the file name:



Private Sub LoadPic(ByVal FileNameSt As String)
Me.ThePic.Load(PixFolderPath & "/" & FileNameSt)
End Sub


Once the user has loaded the picture into the form, they have the option to delete the picture from the file. The code I am trying to do this with is as follows:



Private Sub DelButton_Click(sender As System.Object, e As System.EventArgs) Handles DelButton.Click

Dim Resetter As Boolean
Resetter = False

If Not PixLoaded Then Exit Sub 'User has not selected a file to load pics from
If UBound(PicList) = LBound(PicList) Then PixLoaded = False 'Last pic was deleted
Me.ThePic.Image = Nothing
Me.ThePic.Invalidate()
Me.ThePic.Refresh()
If Not PixLoaded Then
Me.ThePic.Image = My.Resources.NoMorePics 'Out of pics picture
Me.ThePic.Refresh()
GoTo Finisher
End If

'Go to the next image available
If CurNum = UBound(PicList) Then
LoadPic(PicList(LBound(PicList)))
Resetter = True
Else
LoadPic(PicList(CurNum + 1))
End If

Finisher:
My.Computer.FileSystem.DeleteFile(PixFolder & "/" & PicList(CurNum))

If Not PixLoaded Then Exit Sub 'Exits if on last pic

LoadPictures(PixFolder) 'Resets PicList array values
If Resetter Then CurNum = LBound(PicList)
End Sub


My issue comes when the user is trying to delete the last picture out of the file. For some reason I get the "The process cannot access the file 'File Name Here' because it is being used by another process." error on My.Computer.FileSystem.DeleteFile(PixFolder & "/" & PicList(CurNum)). How do I remove the picture file from being used by my form so it can actually be deleted?


Aucun commentaire:

Enregistrer un commentaire