• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 662
  • Last Modified:

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

0
bd9000
Asked:
bd9000
  • 4
  • 3
1 Solution
 
bd9000Author Commented:
Another thing, this may have to do with the OpenFileDialog as the source is changed on the FileOk event.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Let's see the code that shows the OpenFileDialog along with your attempt at changing the image...
0
 
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"
        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

0
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

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

What happens now?
0
 
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).
0
 
Mike TomlinsonMiddle 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...
0
 
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.
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now