Click here to Skip to main content
15,894,017 members
Articles / Programming Languages / C#
Technical Blog

Combine several images to form a single image using C#

Rate me:
Please Sign up or sign in to vote.
2.93/5 (12 votes)
3 Dec 2012CPOL 118.3K   15   31
How to 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:
  5. C#
    private void cmdCombine _Click(object sender, EventArgs e)
    {
    }
  6. Place the following code in the event handler block.
  7. C#
    //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. C#
    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)


Written By
Founder Rebin Infotech
India India
A passionate developer with over 10 years of experience and building my software company code by code. Experience withMS Technologies like .Net | MVC | Xamarin | Sharepoint | MS Project Server and PhP along with open source CMS Systems like Wordpress/DotNetNuke etc.

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. #SOreadytohelp

Comments and Discussions

 
AnswerRe: what is imageLocation? Pin
Member 1075131015-Apr-14 6:42
Member 1075131015-Apr-14 6:42 
GeneralRe: what is imageLocation? Pin
Nitesh Kejriwal16-Apr-14 7:53
professionalNitesh Kejriwal16-Apr-14 7:53 
GeneralRe: what is imageLocation? Pin
Member 1075131016-Apr-14 7:55
Member 1075131016-Apr-14 7:55 
GeneralRe: what is imageLocation? Pin
Nitesh Kejriwal16-Apr-14 8:06
professionalNitesh Kejriwal16-Apr-14 8:06 
GeneralRe: what is imageLocation? Pin
Member 1075131016-Apr-14 9:02
Member 1075131016-Apr-14 9:02 
SuggestionImprovements... Pin
Andrew Rissing3-Dec-12 5:04
Andrew Rissing3-Dec-12 5:04 
GeneralRe: Improvements... Pin
Nitesh Kejriwal3-Dec-12 6:41
professionalNitesh Kejriwal3-Dec-12 6:41 
GeneralRe: Improvements... Pin
Gaurav_Chhabra4-Jul-13 10:44
Gaurav_Chhabra4-Jul-13 10:44 
Hello,
Your solution is helpful but in my case i need do the image processing too ( Because two images have same part of objects) in short i have a folder of images in which two or more images contains same section of focus so i need overlap the area of the images which is same and then at last join.
Will be waiting for the support from your side
Regards,
GeneralRe: Improvements... Pin
Nitesh Kejriwal4-Jul-13 11:09
professionalNitesh Kejriwal4-Jul-13 11:09 
GeneralRe: Improvements... Pin
Gaurav_Chhabra17-Jul-13 6:43
Gaurav_Chhabra17-Jul-13 6:43 

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

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