Trouble with assigning a Picturebox background image at runtime

Cynolycus 260 Reputation points
2023-06-04T09:55:40.8033333+00:00

I want to display an image based on the users selection from a combobox.

First I want it to look for an image in a specified path matching the name that is displayed in the combobox.

If it is not found I then want it to look in the resources for a match and if not found there then an image that represents that no image was found will be displayed. I haven't added the code which displays the image that represents no image was found.

I may have finally worked out how to do it but there is a slight problem where on loading the combobox already has an item selected and so it displays the image found in the specified path, but all other controls such as textboxes, comboboxes, DGVs, lables etc are all white with no data.

Using the combobox and selecting an item that doesn't have an image at the specified path or in the resources corrects the problem and everything works fine after that.

Obviously this is caused by my lack of knowledge which is why I ask if anybody can see why this would happen and how to fix it.

 
 	Private Sub PictureBox1_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox1.Paint
        Dim ImageName As String = ComboBox1.Text & " LOC"
        If System.IO.File.Exists(Path & "\" & ImageName & ".jpg") = True Then
            PictureBox1.BackgroundImage = Image.FromFile(Path & "\" & ImageName & ".jpg")
        ElseIf Not IsNothing(My.Resources.ResourceManager.GetObject(ImageName)) Then
            PictureBox1.BackgroundImage = CType(My.Resources.ResourceManager.GetObject(ImageName), Image)
        End If
    End Sub
Developer technologies | VB
{count} votes

Accepted answer
  1. Anonymous
    2023-06-04T14:13:42.36+00:00

    Hi

    OK, you have it sorted. As I spent a little time to compose an example, I will go ahead and post it.

    This example uses a DeskTop Folder called TestImages and contains some .jpg files (as per details in comments. There are 2 .jpg files in Resources only, and 1 .jpg file in both Resources and in Folder. All just to demo priorities etc.

    I hard coded some names for ComboBox, with all the true files and a couple of non existant files.

    Form1 has PictureBox1 and ComboBox1

    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        ' I used random sized images so need
        ' to make fit the PictureBox
        PictureBox1.SizeMode = PictureBoxSizeMode.StretchImage
    
        ' add some names to CB.
        ' TestImage And NotFound are only
        ' in Resources
    
        ' BOTH image is in Folder and Resources
    
        ' Space, ABS LOC and LightHouse are
        ' only in a File folder
    
        ' the others are non existant
    
        ' all files have jpg extension
        ' Resources has first priority
    
        ComboBox1.Items.AddRange({"TestImage", "XYZnotfound", "Space", "ABCnotfound", "BOTH", "Lighthouse LOC", "FGHnotfound", "ABS LOC"})
      End Sub
      Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        Dim BGimage As Image = Nothing
        If My.Resources.ResourceManager.GetObject(ComboBox1.SelectedItem.ToString) IsNot Nothing Then
          PictureBox1.Image = CType(My.Resources.ResourceManager.GetObject(ComboBox1.SelectedItem.ToString), Image)
        Else
          Dim path As String = IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Desktop, "TestFiles")
    
          Dim fPath As String = IO.Path.Combine(path, ComboBox1.SelectedItem.ToString & ".jpg")
          If IO.File.Exists(fPath) Then
            PictureBox1.Image = Image.FromFile(fPath)
          Else
            PictureBox1.Image = My.Resources.NotFound
          End If
        End If
      End Sub
    End Class
    
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.