Click here to Skip to main content
Click here to Skip to main content

Combine several images to form a single image using C#

By , 3 Dec 2012
Rate this:
Please Sign up or sign in to vote.

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:
  5. private void cmdCombine _Click(object sender, EventArgs e)
    {
    }
  6. Place the following code in the event handler block.
  7. //Change the path to location where your images are stored.
    DirectoryInfo directory=new DirectoryInfo("C:\\MyImages");
    if(directory!=null)
    {
        FileInfo[]files = directory.GetFiles();
        CombineImages(files);
    }
  8. Write the following code after the event handler block.
  9. 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);
            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();
        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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Nitesh Luharuka
Team Leader Independent IT Professional, Consultant
India India
An Independent IT Professional and Consultant on MS Technologies like .Net | MVC | Silverlight | Sharepoint | MS Project Server along with open source CMS Systems like DotNetNuke etc.
 
I am an enthusiastic technocrat who love to debug problems and solve them. I love writing articles on my website in my spare time. Please visit my Website for more details and subscribe to get technology related tips/tricks.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
QuestionWhat is List Pinmemberrajeeshrer15-Apr-14 21:15 
AnswerRe: What is List PinmemberNitesh Luharuka16-Apr-14 7:54 
Questionwhat is imageLocation? PinmemberYasir Babar1-Mar-14 23:58 
AnswerRe: what is imageLocation? PinmemberNitesh Luharuka3-Mar-14 4:24 
GeneralRe: what is imageLocation? PinmemberYasir Babar3-Mar-14 23:26 
GeneralRe: what is imageLocation? PinmemberNitesh Luharuka4-Mar-14 3:36 
GeneralRe: what is imageLocation? PinmemberMember 1075131015-Apr-14 9:04 
AnswerRe: what is imageLocation? PinmemberMember 1075131015-Apr-14 6:42 
GeneralRe: what is imageLocation? PinmemberNitesh Luharuka16-Apr-14 7:53 
GeneralRe: what is imageLocation? PinmemberMember 1075131016-Apr-14 7:55 
GeneralRe: what is imageLocation? PinmemberNitesh Luharuka16-Apr-14 8:06 
GeneralRe: what is imageLocation? PinmemberMember 1075131016-Apr-14 9:02 
SuggestionImprovements... PinmemberAndrew Rissing3-Dec-12 5:04 
GeneralRe: Improvements... PinmemberNitesh Luharuka3-Dec-12 6:41 
GeneralRe: Improvements... PinmemberGaurav_Chhabra4-Jul-13 10:44 
GeneralRe: Improvements... PinmemberNitesh Luharuka4-Jul-13 11:09 
GeneralRe: Improvements... PinmemberGaurav_Chhabra17-Jul-13 6:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140415.2 | Last Updated 3 Dec 2012
Article Copyright 2012 by Nitesh Luharuka
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid