PictureBox on panel in c# window application

How to add multiple images picture box on panel using c# through coding just like this pic you can see pic and tell me
Capture01.PNG
Sharrif HussainSoftware quality AssuranceAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dustin SaundersDirector of OperationsCommented:
You've got a couple of options, ways to do this.  You can load the controls in and space them yourself if you want to have absolute control over where they go:

Panel panel;

        List<PictureBox> pictures;

        public void load()
        {
            int loaded = 0;
            int padding = 4;

            foreach (PictureBox p in pictures)
            {
                p.Location = new System.Drawing.Point(padding + (loaded * (p.Width + padding)), padding);
                panel.Controls.Add(p);
            }
        }

Open in new window


Or, what would be simpler, is to replace your panel with a FlowLayoutPanel and then just add the controls to that.  You can adjust the configuration in the VS IDE.

            FlowLayoutPanel flp = new FlowLayoutPanel();
            foreach (PictureBox p in pictures)
            {
                flp.Controls.Add(p);
            }

Open in new window


Much simpler.
0
Dustin SaundersDirector of OperationsCommented:
So, to give a more detailed explanation in code, I have a folder called C:\test\images and pictures called cat1.jpg - cat5.jpg.

In code:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadImages();
        }

        private string filePath = @"C:\test\images\";

        private void LoadImages()
        {
            FlowLayoutPanel flp = new FlowLayoutPanel
            {
                Size = new Size(this.Width - 20, this.Height - 20),
                Location = new Point(10, 10)
            };

            for (int i = 1; i <= 5; i++)
            {
                PictureBox pb = new PictureBox
                {
                    Image = Image.FromFile(filePath + "cat" + i.ToString() + ".jpg"),
                    Size = new Size(75, 75),
                    SizeMode = PictureBoxSizeMode.StretchImage
                };
                              

                flp.Controls.Add(pb);
            }
            
            this.Controls.Add(flp);
        }
    }

Open in new window


This will load each image into my FlowLayoutPanel and automatically space them.

cats.png
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Dustin SaundersDirector of OperationsCommented:
Answers and examples on how to accomplish the request.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.