Query image in a folder

0

I am creating a program in windows form and in that program I would like to select an option in the combobox and it loads the image in the picturebox. This image will have the name that was selected in the combobox.

EX: combobox = banana it goes in the folder that the photos are and look for the one with the banana name and load it in the picture box.

Can you help me?

code used to save image to folder:

 pictureBox6.Image.Save(@"E:\Programas\Projetos\Imagen\" + textBox1.Text + ".jpg");
    
asked by anonymous 03.07.2017 / 15:01

1 answer

1

To know whenever the user selects a value in the combobox, it is necessary to use the SelectedIndexChanged (or any similar) event.

To load a PictureBox with a file system image, you can use Image.FromFile(string caminhoImagem) .

Sample code below. Obviously taking into account that combobox items are strings and that the combo text contains the image extension.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    var comboBox = (ComboBox)sender;
    string imgSelecionada = (string)comboBox.SelectedItem;

    CarregarImagem(imgSelecionada);
}

private void CarregarImagem(string nomeImagem)
{
    const string pastaRaiz = @"E:\Programas\Projetos\Imagen"; 

    var caminhoImagem = Path.Combine(pastaRaiz, nomeImagem);    

    pictureBox1.Image = Image.FromFile(caminhoImagem);    
}
    
03.07.2017 / 15:10