Click here to Skip to main content
12,450,101 members (51,813 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

39K views
12 bookmarked
Posted

Combine several images to form a single image using C#

, 3 Dec 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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. 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)

Share

About the Author

Nitesh Luharuka
Team Leader Director @ Rebin Infotech
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.

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralMy vote of 2 Pin
George Jonsson1-Feb-16 16:04
professionalGeorge Jonsson1-Feb-16 16:04 
Questionyour code is not working Pin
Mannava Siva Aditya13-Oct-15 2:35
memberMannava Siva Aditya13-Oct-15 2:35 
AnswerRe: your code is not working Pin
Nitesh Luharuka29-Oct-15 0:48
professionalNitesh Luharuka29-Oct-15 0:48 
GeneralError Pin
Durgesh55822-Sep-15 0:53
memberDurgesh55822-Sep-15 0:53 
Questionerror Pin
Member 1168109011-May-15 1:57
memberMember 1168109011-May-15 1:57 
AnswerRe: error Pin
Nitesh Luharuka11-May-15 2:31
memberNitesh Luharuka11-May-15 2:31 
GeneralMy vote of 3 Pin
Aneesh Divakaran10-Jun-14 0:27
professionalAneesh Divakaran10-Jun-14 0:27 
GeneralRe: My vote of 3 Pin
Nitesh Luharuka12-Jun-14 4:17
memberNitesh Luharuka12-Jun-14 4:17 
QuestionWhat is List Pin
rajeeshrer15-Apr-14 21:15
memberrajeeshrer15-Apr-14 21:15 
AnswerRe: What is List Pin
Nitesh Luharuka16-Apr-14 7:54
memberNitesh Luharuka16-Apr-14 7:54 
Questionwhat is imageLocation? Pin
Yasir Babar1-Mar-14 23:58
memberYasir Babar1-Mar-14 23:58 
AnswerRe: what is imageLocation? Pin
Nitesh Luharuka3-Mar-14 4:24
memberNitesh Luharuka3-Mar-14 4:24 
GeneralRe: what is imageLocation? Pin
Yasir Babar3-Mar-14 23:26
memberYasir Babar3-Mar-14 23:26 
GeneralRe: what is imageLocation? Pin
Nitesh Luharuka4-Mar-14 3:36
memberNitesh Luharuka4-Mar-14 3:36 
GeneralRe: what is imageLocation? Pin
Member 1075131015-Apr-14 9:04
memberMember 1075131015-Apr-14 9:04 
AnswerRe: what is imageLocation? Pin
Member 1075131015-Apr-14 6:42
memberMember 1075131015-Apr-14 6:42 
GeneralRe: what is imageLocation? Pin
Nitesh Luharuka16-Apr-14 7:53
memberNitesh Luharuka16-Apr-14 7:53 
GeneralRe: what is imageLocation? Pin
Member 1075131016-Apr-14 7:55
memberMember 1075131016-Apr-14 7:55 
GeneralRe: what is imageLocation? Pin
Nitesh Luharuka16-Apr-14 8:06
memberNitesh Luharuka16-Apr-14 8:06 
GeneralRe: what is imageLocation? Pin
Member 1075131016-Apr-14 9:02
memberMember 1075131016-Apr-14 9:02 
SuggestionImprovements... Pin
Andrew Rissing3-Dec-12 5:04
memberAndrew Rissing3-Dec-12 5:04 
GeneralRe: Improvements... Pin
Nitesh Luharuka3-Dec-12 6:41
memberNitesh Luharuka3-Dec-12 6:41 
GeneralRe: Improvements... Pin
Gaurav_Chhabra4-Jul-13 10:44
memberGaurav_Chhabra4-Jul-13 10:44 
GeneralRe: Improvements... Pin
Nitesh Luharuka4-Jul-13 11:09
memberNitesh Luharuka4-Jul-13 11:09 
GeneralRe: Improvements... Pin
Gaurav_Chhabra17-Jul-13 6:43
memberGaurav_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.

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