August 31, 2012 by Nitesh

Combine several images to form a single image using C#


Today, I was working on a problem where I required to add up two image to form a single image. The following code does the same for you.

1. Place a button from the toolbar on your C# form.
2. Select the button and press F4.
3. Change the name to cmdCombine and text as Combine Images.
4. Double click on the button to generate its click handler as follows:

private void cmdCombine _Click(object sender, EventArgs e)

5. Place the following code in the event handler block.
//Change the path to location where your images are stored.
DirectoryInfo directory=new DirectoryInfo("C:\\MyImages");
FileInfo[]files = directory.GetFiles();

6. Write the following code after the event handler block.

private void CombineImages(FileInfo[] files)
//change the location to store the final image.
string finalImage = @"C:\\MyImages\\FinalImage.jpg";
List imageHeights = new List();
int nIndex = 0;
int width = 0;
foreach (FileInfo file in files)
Image img = Image.FromFile(file.FullName);

width += img.Width;


int height = imageHeights[imageHeights.Count - 1];

Bitmap img3 = new Bitmap(width, height);
Graphics g = Graphics.FromImage(img3);


foreach (FileInfo file in files)
Image img = Image.FromFile(file.FullName);
if (nIndex == 0)
g.DrawImage(img, new Point(0, 0));
width = img.Width;
g.DrawImage(img, new Point(width, 0));
width += img.Width;


img3.Save(finalImage, System.Drawing.Imaging.ImageFormat.Jpeg);

imageLocation.Image = Image.FromFile(finalImage);

You need to include the System.Drawing.Imaging namespace to make this code work.

Please change the directory path where your images are stored and where you want to generate the final image.

  • thefightbetting

    Excellent, good work!

  • Andrew

    Great article, just what I wanted to find.

  • doudoune

    This article is a great help to me!

  • Gaurav Chhabra


    When i am taking help from you code and the moment oi execute i am getting two errors in the two statements. They are –

    Error1 Operator ‘-‘ cannot be applied to operands of type ‘method group’ and ‘int’ inside
    int height = imageHeights[imageHeights.Count – 1]; in part of [imageHeights.Count – 1]

    and another error is

    Error2 The name ‘imageLocation’ does not exist in the current context inside
    imageLocation.Image = Image.FromFile(finalImage);

    even i am confuse from where you are using imageLocation ..

    I will be waiting for your reply

    • Gaurav,

      Below is solution to the errors –
      Error1 – Please check if Count is method or property.
      Error2 – imageLocation is the name of the PictureBox control on the form.

  • Onqwah

    Helped me a lot, thank you for sharing.

  • kas

    Where did you declare the imageLocation? You used that at the very end of the code

    • imageLocation is a Image control used on Winforms project. You can name it anything

Support us!

If you like this site please help and make click on any of these buttons!

Powered by WordPress Popup