---
title: "Displaying an array of images in pictureBox C#"  
description: "In this article I have describe how to display an array of images in single pictureBox using C#. Normally users used multiple pictureBox for multiple"  
author: "AVADHESH PATEL"  
published: 2013-02-14  
updated: 2020-05-22  
canonical: https://www.mindstick.com/articles/1144/displaying-an-array-of-images-in-picturebox-c-sharp  
category: "c#"  
tags: ["c#", "pics"]  
reading_time: 5 minutes  

---

# Displaying an array of images in pictureBox C#

In this article, I have described how to display an array of images in single PictureBox using C#.

Normally users used multiple PictureBox for multiple images.

Before the solution, I have shown you an effect of multiple images into multiple picture boxes.\

##### Figure 1:

![Displaying an array of images in pictureBox C#](https://www.mindstick.com/mindstickarticle/73eb92cb-7e33-4de9-bf52-4bf5314f6cda/images/2b218ee8-774a-4489-94e1-0c4f1645f867.png)

Now, I going to describe, how to use multiple images into single PictureBox.

**Step 1:** Open a window form (e.g. Array of Image) in Visual Studio with C# and place one button (e.g. Display Image) and one PictureBox (e.g. pictureBox1) [**control as below image**](https://www.mindstick.com/articles/595/resizing-image-in-c-sharp) (e.g. Figure 2).

##### Figure 2:

![Displaying an array of images in pictureBox C#](https://www.mindstick.com/mindstickarticle/73eb92cb-7e33-4de9-bf52-4bf5314f6cda/images/a7f25529-d42b-44ec-a3a9-c7fcc492338d.png)

**Step 2:** Use below namespace in your window form’s .cs file.

```
using System.Drawing;
using System.IO;
using System.Collections.Generic;
```

Step 3: Generate button click event and paste below the line of code

```
//Change the path to location where your images are stored.
            DirectoryInfo directory = new DirectoryInfo(@"D:\ImageDemo\Images");
            if (directory != null)
            {
                FileInfo[] files = directory.GetFiles();
                CombineImages(files);
            }
```

**Note:** In the above code, changed the existing path (e.g. **D:\ImageDemo\Images**) from your image path folder.

This folder should be possessing at least two images for better understanding and all images should be the same damnations.

**Step 4:** Place below the line of code outside of the button click event. This line of code converts images into a single image.

```
private void CombineImages(FileInfo[] files)
        {
            try
            {
                //change the location to store the final image.
                string finalImage = @"D:\FinalImage\Flower.jpg";
                List<int> imageHeights = new List<int>();
                int nIndex = 0;
                int width = 0;
                foreach (FileInfo file in files)
                {
                    Image img = Image.FromFile(file.FullName);
                    imageHeights.Add(img.Height);
                    width += img.Width;
                    img.Dispose();
                }
                imageHeights.Sort();
                int height = imageHeights[imageHeights.Count - 1];
                Bitmap img3 = new Bitmap(width, height);
                Graphics g = Graphics.FromImage(img3);
                g.Clear(SystemColors.AppWorkspace);
                foreach (FileInfo file in files)
                {
                    Image img = Image.FromFile(file.FullName);
                    if (nIndex == 0)
                    {
                        g.DrawImage(img, new Point(0, 0));
                        nIndex++;
                        width = img.Width;
                    }
                    else
                    {
                        g.DrawImage(img, new Point(width, 0));
                        width += img.Width;
                    }
                    img.Dispose();
                }
                g.Dispose();
                img3.Save(finalImage, System.Drawing.Imaging.ImageFormat.Jpeg);
                img3.Dispose();
                pictureBox1.Image = Image.FromFile(finalImage);
            }
            catch
            {
 
            }
        }
```

**Note:** In the above code, replace the default path (e.g. **D:\FinalImage\Flower.jpg**) from where you want to [**save a dynamically generated images**](https://www.mindstick.com/articles/530/webbrowser-control-in-wpf).

This path will be different from the image sources path which we have given in step 3.

**Step 5:** Save your application and execute it. Click on the button “Display Image” and see an output as below.

##### Figure 3:

![Displaying an array of images in pictureBox C#](https://www.mindstick.com/mindstickarticle/73eb92cb-7e33-4de9-bf52-4bf5314f6cda/images/59e02321-06f4-40d6-844e-1ec1a79124f8.png)

---

Original Source: https://www.mindstick.com/articles/1144/displaying-an-array-of-images-in-picturebox-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
