1

I've been having problems with clearing a picture box. When a user clicks on the upload logo button the system copies whatever image file is selected by the user and copied into the repository. When the user clears the logo the image in the picture box removes the file from the repository and opens the default image. I am getting an exception error whenever I click on the clear logo button.

Private Sub cmdClearLogo_Click(sender As Object, e As EventArgs) Handles cmdClearLogo.Click
    If strLogo IsNot "C:\Fowl\Images\Image Not Available.jpg" Then
        picLogo.Image = Nothing
        picLogo.Dispose()
        My.Computer.FileSystem.DeleteFile(strLogo)
        strLogo = "C:\Fowl\Images\Image Not Available.jpg"
        picLogo.BackgroundImage = Image.FromFile(strLogo)
    End If
End Sub

    Private Sub cmdUploadLogo_Click(sender As Object, e As EventArgs) Handles cmdUploadLogo.Click
    Dim strSource As String
    My.Computer.FileSystem.CreateDirectory("C:\Fowl\Repository")
    ofdLogo.ShowDialog()
    If DialogResult.OK Then
        strSource = ofdLogo.FileName
        strLogo = "C:\Fowl\Repository\" + lblIDNO.Text + "l.jpg"
        My.Computer.FileSystem.CopyFile(strSource, strLogo)
        picLogo.BackgroundImage = Image.FromFile(strLogo)
    End If
End Sub
  • What exception are you getting and on which line? – UnhandledExcepSean Feb 13 '20 at 18:14
  • 3
    `If strLogo IsNot "C:\Fowl\Images\Image Not Available.jpg"` - this is not how you test strings for equality. – GSerg Feb 13 '20 at 18:15
  • 3
    1. Any time you're “getting an exception error” you [need to tell us what the error is](https://idownvotedbecau.se/noexceptiondetails/). 2. Any time a method is acting unexpectedly the first thing you should do is [read the documentation](https://learn.microsoft.com/en-us/dotnet/api/system.drawing.image.fromfile). If you had done that you would have read that `Image.FromFile` opens the file for reading and **leaves the file open** so if you no longer need it you have to [close the file or dispose the image](https://stackoverflow.com/questions/788335/). – Dour High Arch Feb 13 '20 at 18:23
  • the exception is occurring at "My.Computer.filesystem.deletefile(strlogo)" File is used by another process. "If strLogo is not...." is only being used so the program doesn't remove the default image. – Curt Anderson Feb 13 '20 at 18:27
  • 4
    You can load the Image bytes and use a MemoryStream to store the bytes. Then call `picLogo.Image = Image.FromStream(new MemoryStream(File.ReadAllBytes([Image Path]), true))`. Btw, call `picLogo.Image.Dispose()` then `picLogo.Image = nothing`. The latter is used to simply *clear* the current *view*. The Image file is untouched. You can move it, delete it... – Jimi Feb 13 '20 at 18:35

1 Answers1

0

Finally figured it out:

Private Sub cmdClearLogo_Click(sender As Object, e As EventArgs) Handles cmdClearLogo.Click
    Dim tmpString As Image
    Dim tmpFile As String
    If strLogo IsNot "C:\Fowl\Images\Image Not Available.jpg" Then
        tmpFile = strLogo
        tmpString = picLogo.BackgroundImage
        tmpString.Dispose()
        strLogo = "C:\Fowl\Images\Image Not Available.jpg"
        picLogo.BackgroundImage = Image.FromFile(strLogo)
        My.Computer.FileSystem.DeleteFile(tmpFile)
    End If
End Sub
Private Sub cmdUploadLogo_Click(sender As Object, e As EventArgs) Handles cmdUploadLogo.Click
    Dim strSource As String
    My.Computer.FileSystem.CreateDirectory("C:\Fowl\Repository")
    ofdLogo.ShowDialog()
    If DialogResult.OK Then
        strSource = ofdLogo.FileName
        strLogo = "C:\Fowl\Repository\" + lblIDNO.Text + "l.jpg"
        My.Computer.FileSystem.CopyFile(strSource, strLogo)
        picLogo.BackgroundImage = Image.FromFile(strLogo)
    End If
End Sub