PictureBox Control does not refresh with new file source (dotnet bug?)

I have an issue with the PictureBox control in VB.NET (2.0) not loading an image the second time around.  It loads the first one fine, but when the "Image.FromFile" method points to a new picture path, it never does (or never loads or re-loads the picture).
Refresh() method does not do anything, neither does refreshing the form.
Setting the .Image property to "nothing" before re-assigning it, has no effect, either.

The PictureBox control loads only once, then never loads again no matter what.
The file type is a JPEG and the Operating System is Windows Vista Ultimate SP1, I am debugging in VS2008 Standard (I figure this is a bug with the FromFile property for this OS or VS or DotNet3.5, but I need a workaround).
Any ideas?
Me.PictureBox1.Image = Nothing 'executes, but does not set it to nothing
Me.PictureBox1.Image = Image.FromFile(finalmediumPath) 'only works once
Me.PictureBox1.Refresh() 'redraws the first image loaded, changing image has no effect
Me.Refresh 'worthless - redraws form, but first image always remains

Open in new window

Who is Participating?
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
"I learned that Image.FromFile(...) locks the file and it may take some time to unlock it"

It's not just a matter of "some time"...this is a known limitation of Image.FromFile().

Straight from MSDN: http://msdn.microsoft.com/en-us/library/stf701f5.aspx

    "The file remains locked until the Image is disposed."

So as long as the PictureBox is displaying the Image ...the file is locked.  I'm suprised you didn't get any Exceptions...
bd9000Author Commented:
Another thing, this may have to do with the OpenFileDialog as the source is changed on the FileOk event.
Mike TomlinsonMiddle School Assistant TeacherCommented:
Let's see the code that shows the OpenFileDialog along with your attempt at changing the image...
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

bd9000Author Commented:

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Me.OpenFileDialog1.InitialDirectory = My.Settings.LocalUserLargeImagePath
        Me.OpenFileDialog1.FileName = "*.jpg"
    End Sub
    Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        Dim rawimagepath As String = appPath & "\" & My.Settings.LocalRawImageRelativePath & "\" & Me.OpenFileDialog1.SafeFileName
        Me.txtRawImageFilePath.Text = rawimagepath
        'need to copy the file from the raw image path (and resize to large) 
        File.Copy(Me.OpenFileDialog1.FileName, rawimagepath, True)
        'crop image
        'now resize it 3 times large, medium, small
        Dim largedimX, largedimY, mediumdimX, mediumdimY, smalldimX, smalldimY As Integer
        largedimX = My.Settings.LocalLargeImageSize
        largedimY = My.Settings.LocalLargeImageSize
        mediumdimX = My.Settings.LocalMediumImageSize
        mediumdimY = My.Settings.LocalMediumImageSize
        smalldimX = My.Settings.LocalSmallImageSize
        smalldimY = My.Settings.LocalSmallImageSize
        Dim NewFileName As String = Me.SKUTextBox.Text.Trim & ".jpg"
        Dim finalLargePath As String = appPath & "\" & My.Settings.LocalLargeImageRelativePath & "\" & NewFileName
        ImageHandling.ResizeImage(rawimagepath, finalLargePath, largedimY, largedimX)
        Dim finalmediumPath As String = appPath & "\" & My.Settings.LocalMediumImageRelativePath & "\" & NewFileName
        ImageHandling.ResizeImage(rawimagepath, finalmediumPath, mediumdimY, mediumdimX, , 90)
        Dim finalsmallPath As String = appPath & "\" & My.Settings.LocalSmallImageRelativePath & "\" & NewFileName
        If Me.rbSoldAsCarton.Checked Or Me.rbSoldAsLot.Checked Then
            ImageHandling.ResizeImage(rawimagepath, finalsmallPath, smalldimY, smalldimX, 45, 80)
            ImageHandling.ResizeImage(rawimagepath, finalsmallPath, smalldimY, smalldimX, , 80)
        End If
        Me.PictureBox1.Image = Image.FromFile(finalmediumPath)
    End Sub

Open in new window

Mike TomlinsonMiddle School Assistant TeacherCommented:
Make sure you are generating a VALID image by seeing if it opens with your default image viewer:

        Process.Start(finalmediumPath) ' Open up the Image with the default Image Viewer
        Me.PictureBox1.Image = Image.FromFile(finalmediumPath)

What happens now?
bd9000Author Commented:
Thanks, it's a valid image.
It turns out that creating a stream and loading the picturebox with it works every time.
I'm not sure why this is, but I needed to use a stream just in case the user decided to change their mind on the same dialog window.  I learned that Image.FromFile(...) locks the file and it may take some time to unlock it (I suspect that was somehow related to the problem in another sub).
bd9000Author Commented:
Turns out I did get exceptions, but the Throw Exception did not, for whatever reason, actually push the exception up the stack, but putting a break in the handler, I found it.
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.