We help IT Professionals succeed at work.

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

Medium Priority
705 Views
Last Modified: 2013-11-26
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

Comment
Watch Question

Author

Commented:
Another thing, this may have to do with the OpenFileDialog as the source is changed on the FileOk event.
Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
Let's see the code that shows the OpenFileDialog along with your attempt at changing the image...

Author

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"
        Me.OpenFileDialog1.ShowDialog()
    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
        ImageHandling.CropImageToSquare(rawimagepath)
 
        '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
            'create 
            ImageHandling.ResizeImage(rawimagepath, finalsmallPath, smalldimY, smalldimX, 45, 80)
        Else
            ImageHandling.ResizeImage(rawimagepath, finalsmallPath, smalldimY, smalldimX, , 80)
        End If
 
        Me.PictureBox1.Image = Image.FromFile(finalmediumPath)
        Me.PictureBox1.Refresh()
    End Sub

Open in new window

Mike TomlinsonHigh School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009

Commented:
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)
        Me.PictureBox1.Refresh()

What happens now?

Author

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).
High School Computer Science, Computer Applications, Digital Design, and Mathematics Teacher
CERTIFIED EXPERT
Top Expert 2009
Commented:
"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...

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.