Trying to use a default image - please help!

Shaggycat

Member
Joined
Mar 22, 2006
Messages
8
Programming Experience
Beginner
I am populating some pictureboxes from a combo box selection, that works until there is a jpg in the folder that doesn't exist. For some reason I can't get the default jpg to come up, can someone help me please?
Thanks in advance for the assistance.....

VB.NET:
Private Sub cmbTime_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbTime.SelectedIndexChanged
        
        Dim folder As String = "C:\Projects\Dar\Queue Review Files\" & DateTime.ParseExact(cmbDate.SelectedItem.ToString, "MMMM d, yyyy", Nothing).ToString("M-d") & "\Cam {0}\Cam{0}" & cmbTime.Text.Substring(cmbTime.Text.IndexOf("-"))

        For i As Integer = 1 To 8
            Dim pcb As PictureBox = ApplicationControls.FindControl(Me, "pcbCam" & i)
            If Not pcb Is Nothing Then
                If File.Exists(String.Format(folder, 1)) Then
                    pcb.Image = Image.FromFile(String.Format(folder, i))

                Else
                    MsgBox("I am right here")
                   pcb.Image = Image.FromFile("C:\Projects\Dar\Queue Review Files\noimage.jpg")
                End If

            End If

        Next i
     
    End Sub
 
So what actually happens? Do you get an error message? Does nothing appear? You are presumably not aware that calling Image.FromFile locks the file until that Image object is Disposed, so you cannot create another Image from the same file. You should probably be compiling your default image into your app as a resource. You then create a single Image object from that resource and display it as needed. As for the other images, you should be calling Dispos on the current Image before creating and displaying another. Otherwise every file you open will be locked until you close your application.
 
Hi there..... I got it working thank you for helping.
If File.Exists(String.Format(folder, 1)) Then
needed to be
If File.Exists(String.Format(folder, i)) Then
 
Back
Top