Changing Images from a PictureBox C # - Windows Form Application

0

Good Afternoon Personal,

asked by anonymous 31.07.2018 / 20:32

2 answers

4

You can grab the files from the folder and move the next button as follows. I left a method for if you want to make a back button too.

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

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]);
}
    
31.07.2018 / 21:40
0

You should have a list with all paths of the images in the directory you have 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 hand is easy, start by displaying index 0 from the list:

string caminhoImagem = imagens[0];

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

    
31.07.2018 / 21:32