Import and display images one by one

I have a C # window form for importing and displaying multiple images.

I can import multiple images and display the first image, but I am having trouble displaying the images one by one.

Program stream: the user presses a button, then selects several images. After that, the first image should be displayed in the image window. When the user clicks the β€œnext image” button, the next image should be displayed.

The first image can be displayed on the image panel, but has no idea about displaying them one at a time. Is there any configuration to achieve this goal or its implementation through coding. Thanks to everyone.

My coding:

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
        InitializeOpenFileDialog();
    }

    private void InitializeOpenFileDialog()
    {
        // Set the file dialog to filter for graphics files. 
        this.openFileDialog1.Filter =
            "Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" +
            "All files (*.*)|*.*";

        // Allow the user to select multiple images. 
        this.openFileDialog1.Multiselect = true;
        this.openFileDialog1.Title = "My Image Browser";
    }

    private void SelectFileButton_Click(object sender, EventArgs e)
    {
        DialogResult dr = this.openFileDialog1.ShowDialog();
        if (dr == System.Windows.Forms.DialogResult.OK)
        {
            // Read the files 
            foreach (String file in openFileDialog1.FileNames)
            {
                // Create a PictureBox. 
                    PictureBox pb = new PictureBox();
                    Image loadedImage = Image.FromFile(file);
                    pb.Height = loadedImage.Height;
                    pb.Width = loadedImage.Width;
                    pb.Image = loadedImage;
                    flowLayoutPanel1.Controls.Add(pb);
            }

        }
    }
}
+4
1

, .

PictureBox , . , .

:

PictureBox ( pictureBox1), , .

, :

private List<Image> loadedImages = new List<Image>();
private int currentImageIndex;

" " :

private void SelectFileButton_Click(object sender, EventArgs e)
{
    DialogResult dr = this.openFileDialog1.ShowDialog();
    if (dr == System.Windows.Forms.DialogResult.OK)
    {
        loadedImages.Clear();

        // Read the files 
        foreach (String file in openFileDialog1.FileNames)
        {
            // Create a PictureBox. 
            loadedImages.Add(Image.FromFile(file));
        }

        if (loadedImages.Count > 0)
        {
            currentImageIndex = 0;
            pictureBox1.Image= loadedImages[currentImageIndex];
        }
    }
}

, , "/" , :

// Mod function to support negative values (for the back button).
int mod(int a, int b)
{
    return (a % b + b) % b;
}

// Show the next picture in the PictureBox.
private void button_next_Click(object sender, EventArgs e)
{
    currentImageIndex = mod(currentImageIndex + 1, loadedImages.Count);
    pictureBox1.Image = loadedImages[currentImageIndex];
}

// Show the previous picture in the PictureBox.
private void button_prev_Click(object sender, EventArgs e)
{
    currentImageIndex = mod(currentImageIndex - 1, loadedImages.Count);
    pictureBox1.Image = loadedImages[currentImageIndex];
}
+3

Source: https://habr.com/ru/post/1526722/


All Articles