Query image in a folder

Asked

Viewed 702 times

0

I am creating a program in windows form and in this program I would like to select an option in the combobox and it load 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 are the photos and looks for the one that is named banana and loads it in the picture box.

Can you help me?

code used to save image in folder:

 pictureBox6.Image.Save(@"E:\Programas\Projetos\Imagen\" + textBox1.Text + ".jpg");
  • Yes, we can help. What is the question?

  • I would like to know how to carry out this procedure

1 answer

1


To know whenever the user selects a value in the combobox you will need to use the event SelectedIndexChanged (or any similar).

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

Sample code below. Obviously taking into consideration that the combobox 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);    
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.