Folder Browser Dialog to work in picturebox

AndrewShev05

Member
Joined
Dec 9, 2021
Messages
12
Programming Experience
Beginner
Hello, I am a bare-bones beginner with very little to no knowledge of C# programming, also new to the forum. I want to make an image viewer to load a whole folder of images and be able to scroll through them. I met a large speedbump that I cannot figure out. I am using the folder browser dialog because I need to select the whole folder. Below is the code I have, but I can't get it to work. I need the folder to be selected through the folder browser dialog and placed into the picture box. Please help.


private void loadButton_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.Load(folderBrowserDialog1.SelectedPath);
}
}
 

Attachments

  • Screenshot 2021-12-09 122119.jpg
    Screenshot 2021-12-09 122119.jpg
    17.1 KB · Views: 22
A picture box can only display one image. To display multiple images, you either need multiple picture boxes, or use a list view in large or medium thumbnail mode, and then load the images one at a time into the appropriate UI object(s).
 
A picture box can only display one image. To display multiple images, you either need multiple picture boxes, or use a list view in large or medium thumbnail mode, and then load the images one at a time into the appropriate UI object(s).
Is there any other type of "boxes" that will allow me to do this? This is for a project, and it needs to be one box, with scroll arrows, kinda similar to Microsofts image viewer. I'm really lost and stressed, pls help.
 
As I said in post #2: "or use a list view".

 
As I said in post #2: "or use a list view".

The project states I can't do that. Is there a way to load the next image non-manually whenever i "scroll" to the next one?
 
Beyond those, there is nothing built into Windows that has the image viewer style UI where there is only one image displayed, but there are left-right buttons for going between the images, and scrollbars when you've zoomed in.
 
Is there any other type of "boxes" that will allow me to do this? This is for a project, and it needs to be one box, with scroll arrows, kinda similar to Microsofts image viewer. I'm really lost and stressed, pls help.
Simplest except Listview is a FlowLayoutPanel that you add one PictureBox to for each image, benefit is you get that flow and scroll (just set AutoScroll in designer) and don't have to position each PictureBox. Something like this:
C#:
flowLayoutPanel1.SuspendLayout();
foreach (var file in Directory.GetFiles(folderBrowserDialog1.SelectedPath))
{
    var pb = new PictureBox { ImageLocation = file, SizeMode = PictureBoxSizeMode.Zoom };
    flowLayoutPanel1.Controls.Add(pb);
}
flowLayoutPanel1.ResumeLayout();
 
it needs to be one box, with scroll arrows, kinda similar to Microsofts image viewer.

The project states I can't do that.
Does it? A ListView is one box with scroll arrows. It provides more of the infrastructure for you than a FlowLayoutPanel but it's still one box. If this is a homework assignment and it places limitations then you should be explicit about that.
 
Beyond those, there is nothing built into Windows that has the image viewer style UI where there is only one image displayed, but there are left-right buttons for going between the images, and scrollbars when you've zoomed in.
Is there a way to have one picture box, that shows one picture at a time, but is able to go through many photos without having to manually select a new picture each time? I want to load a folder of photos and be able to look through each of them one at a time, without manually selecting a new file each time. Do you understand what I am saying? Do you want me to show what the project wants me to do? I don't want yall to do my homework, that's why I asked 1 question because I was confused about how to get it to work.
 
It is possible, get the list of images and keep track of current index, increment this index and show the corresponding image.
You could have next/previous buttons that do it, or for example a timer that show a new image every couple of seconds.
pseudo code:
private images, index

button click
    select folder
    set images (paths)
    set index 0

timer elapsed
    increment index
    show images(index)
 
Based on those instructions, you definitely do not want the Windows Photo Viewer style which has it's left and right button only show up when you hover over the picture. The instructions and screenshot indicate that you have two separate buttons: left and right. That means that you need to implement stuff yourself, and not depend on some kind of built-in control.
 
Based on those instructions, you definitely do not want the Windows Photo Viewer style which has it's left and right button only show up when you hover over the picture. The instructions and screenshot indicate that you have two separate buttons: left and right. That means that you need to implement stuff yourself, and not depend on some kind of built-in control.
Where do I start, like I said previously, completely new to C# and overall programming Now I am even more scared.
 
I must not fear. Fear is the mind-killer. Fear is the little-death that brings total obliteration. I will face my fear. I will permit it to pass over me and through me. And when it has gone past, I will turn the inner eye to see its path. Where the fear has gone, there will be nothing. Only I will remain.

- Frank Herbert

Here's some pseudo code:
C#:
class PhotoGalleryForm : Form
{
    int _currentIndex;
    string [] _photoFileNames;

    void loadButton_Click( ... )
    {
        show folder browser dialog
        if (OK was pressed on dialog)
        {
            var list = new List<string>(Directory.GetFiles(folder browser dialog selected path))
            remove files which are not image files from the list
            _photoFileNames = list.ToArray();
            Update form title to include the path
            ShowPhoto(0);
        }
    }

    void leftButton_Click( ... )
    {
        ShowPhoto(_currentIndex - 1);
    }

    void rightButton_Click( ... )
    {
        ShowPhoto(_currentIndex + 1);
    }

    void ShowPhoto(int newIndex)
    {
        if (_photoFileNames is null or _photoFileNames.Length)
        {
            disable leftButton
            disable rightButton
            indicator.Text = "0 / 0";
            return;
        }

        newIndex = max(newIndex, 0);
        _currentIndex = min(newIndex, _photoFileNames.Length - 1);
        indicator.Text = string.Format("{0} = {1}", _currentIndex + 1, _photoFileNames.Length);

        photoPictureBox.Image = new Bitmap(_photoFileNames[_currentIndex]);

        if (_currentIndex > 0)
            enable leftButton
        else
            disable leftButton

        if (_currentIndex < _photoFileNames.Length - 1)
            enable rightButton
        else
            disable rightButton
    }
}
 
Last edited:
Back
Top Bottom