Changing pictures from a Picturebox C# - Windows Form Aplication

Asked

Viewed 1,275 times

0

Boa Tarde Personal,

The thing is, I’m working with Windows form and creating a simple application that has a "load" button that opened a window to select an image in the picturebox, the code looked like this:

private void carregarToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //verifica se a imagem foi carregada 
        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            //seleciona a imgaem e vincula a mesma ao pictureBox chamado "exibirImagem"
            exibirImagen.ImageLocation = openFileDialog1.FileName;
            //carrega a imagem selecionada no Picture box "exibirImagem"
            exibirImagen.Load();
            //salva o caminho da imagem selecionada em uma string
            caminhoImagem = openFileDialog1.FileName;
        }
    }

up here to load the image all right. The doubt is as follows, how can I "move" to the next image, for example, assuming that inside the folder I selected the image1.jpg there is also the imagem2.jpg, I want to click a button called next it loads the imagem2.jpg in the picturebox. If anyone can help, of any idea, post a link that helps already thank.

3 answers

4


You can take the files from the folder and pass the next button as follows. I left a method in case you want to make a back button too.

Add a variable to save the index of the image being shown.

private int _indiceImagem = 0;
private string[] _imagens = Directory.GetFiles(caminhoFotos, "*.jpg", SearchOption.TopDirectoryOnly);

public void PassarParaProximaFoto()
{
    _indiceImagem++;

    if (_indiceImagem > _imagens.Length - 1)
         _indiceImagem = 0;

    pictureBox.Image = Image.FromFile(_imagens[_indiceImagem]);
}

public void VoltarParaFotoAnterior()
{
    _indiceImagem--;

    if (_indiceImagem < 0)
         _indiceImagem = _imagens.Length - 1;

    pictureBox.Image = Image.FromFile(_imagens[_indiceImagem]);
}
  • Hello Pedro, thank you very much for your answer, solved my problem. I just had to make a few adjustments as needed, but that’s exactly what I was looking for, thank you again.

  • For nothing, any doubt I am at your disposal.

1

If you want to make a slid without button according to the images in the directory you put, with component timer configured according to seconds you set, and just do this method right below and put the method in the event of the timer:

Method:

private void CarregarImagemSlider()
    {
        try
        {
            //Declare uma varial fora do scopo do metodo na class principal com nome ImagemNumero = 1;
            string s = @"C:\ImagensSlider\"; //Diretorio das imagens
            System.IO.DirectoryInfo d = new System.IO.DirectoryInfo(s);
            int files;
            files = d.GetFiles().Length;//Pega a quantidade de imagens no diretorio
            if (ImagemNumero == files)//As imagens deve ser renomeadas com numero 1.png,2.png.... na sequencia que você quiser
            {
                ImagemNumero = 1;
            }
            pictureBoxSlider.ImageLocation = string.Format(@"C:\ImagensSlider\{0}.jpg", ImagemNumero);
            ImagemNumero++; //Passa pra próxima imagem depois de percorrer os segundos do timer..
        }
        catch (Exception)
        {

            //Codigo exception aqui Ex: messagembox
        }

    }

0

You should have a list of all the image paths in the directory you selected.

something like:

DirectoryInfo directoryInfo = new DirectoryInfo("DiretorioAqui");

        List<string> imagens = directoryInfo.GetFiles().Where(s => s.Extension.ToLower() == "png" || s.Extension.ToLower() == "jpg").Select(s => s.FullName).ToList();

Having this in hands becomes easy, start by displaying the index 0 of the list:

string caminhoImagem = imagens[0];

By clicking "Next", for example, Voce displays index 1, and so on. This index must be controlled by a variable, which you will increment or decrease as needed.

  • Hello Philip, thank you for your answer, I have one more question, there in the part of the code where you put ("Directory here"), in case I get this directory through my variable openFileDialog1? if yes, as I can do, because so far I have only managed to recover the full path up to the image, but "just" the path of the directory does not. Thanks again for the help.

  • 1

    Pode usar um FolderBrowserDialog: &#xA; FolderBrowserDialog fbd = new FolderBrowserDialog();&#xA;&#xA; fbd.ShowDialog();&#xA;&#xA; string caminho = fbd.SelectedPath;

Browser other questions tagged

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