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

Posted on 2009-02-18
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

Question by:bd9000

    Author Comment

    Another thing, this may have to do with the OpenFileDialog as the source is changed on the FileOk event.
    LVL 85

    Expert Comment

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

    Author Comment


        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

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    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?

    Author Comment

    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).
    LVL 85

    Accepted Solution

    "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:

        "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...

    Author Comment

    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.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
    The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    how to add IIS SMTP to handle application/Scanner relays into office 365.

    730 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now