1

I am making a simple app which will show the images from a specific folder on a winform load. I've done the "showing part" with using openfileDialog and it's working fine, where the user has to select images each time.I want to make it more automatic that user doesn't have to select the files, It should select the files automatically.

Target Folder is static(It'll remain same) while it contains multiple images. How can I obtain all of the image files from the directory?

I am using this code.

 string[] path = Directory.GetFiles("Cards");
            foreach (string filepath in path)
            {
                string[] files = filepath;



                int x = 20;
                int y = 20;
                int maxheight = -1;
                foreach (string img in files)
                {
                    PictureBox pic = new PictureBox();
                    pic.Image = Image.FromFile(img);
                    pic.Location = new Point(x, y);
                    pic.Size = new Size(200, 200);
                    pic.SizeMode = PictureBoxSizeMode.Zoom;
                    x += pic.Width + 10;
                    maxheight = Math.Max(pic.Height, maxheight);
                    if (x > this.ClientSize.Width - 100)
                    {
                        x = 20;
                        y += maxheight + 10;
                    }
                    this.panelImages.Controls.Add(pic);

                }
            }

but stuck at string[] files = filepath; here. Please guide me where I am making any mistake. Please let me know if anybody needs more info. Thanks in advance.

1 Answer 1

2

Consider changing:

string[] path = Directory.GetFiles("Cards");
            foreach (string filepath in path)
            {
                string[] files = filepath;

to:

string[] files = Directory.GetFiles("Cards");

Then your later:

foreach (string img in files)

will iterate over all file in the Cards folder.

There are some dangers in passing in just "Cards" as a parameter, as per the docs:

Relative path information is interpreted as relative to the current working directory.

It would be better, if possible, to pass an absolute path.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.