简体   繁体   中英

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?

I prefer to use FileStream instead of .Load

Dim strImageFileName As String
strImageFileName = PixFolderPath & "/" & FileNameSt
Dim fs As System.IO.FileStream
fs = New System.IO.FileStream(strImageFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read)
ThePic.Image = System.Drawing.Image.FromStream(fs)
fs.Close()

So you will not need to release it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM