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?
m9s999Asked:
Who is Participating?
 
Mike TomlinsonMiddle 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)
            tempImg.Dispose()
        End If
    End Sub

~IM
0
 
bramsquadCommented:
Me.PictureBox1.Refresh()

~b
0
 
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.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

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

~IM
0
 
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")
0
 
m9s999Author Commented:
This works. Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.