PictureBox Not Releasing Image Files

I am using a PictureBox on an employee information form to display a JPG picture. The form has a combo box on it to switch employees. When I change to the next employee, the previous picture file is not being release. If I try to do anything with it, it says it is in use. Usually after a while it will release on its own. Is there any way to make it release faster?
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
You can use code like this to prevent a file lock on the image:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If OpenFileDialog1.ShowDialog = DialogResult.OK Then
            Dim tempImg As Image = Image.FromFile(OpenFileDialog1.FileName)
            PictureBox1.Image = New Bitmap(tempImg)
        End If
    End Sub


m9s999Author Commented:
Refresh didn't help.

The picturebox is displaying the new picture right away. It is just keeping the previous files open for a while.
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

Mike TomlinsonMiddle School Assistant TeacherCommented:
How are you creating an image from the file and displaying it in the PictureBox?

m9s999Author Commented:
I don't have the code in front of me right now, but I think the syntax i'm using is:
PictureBox1.Image = Image.FromFile("PathToPicture")
m9s999Author Commented:
This works. Thanks
All Courses

From novice to tech pro — start learning today.