Click here to Skip to main content
15,860,972 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 117.5K   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

 
GeneralMy vote of 2 Pin
snorkie31-Oct-23 3:03
professionalsnorkie31-Oct-23 3:03 
QuestionError Pin
Member 1468563110-Mar-20 21:42
Member 1468563110-Mar-20 21:42 
Questiondisplaying a combined image file in c# Pin
Member 1329399717-Sep-17 22:59
Member 1329399717-Sep-17 22:59 
QuestionCombine several images to form a single image using silverlight vb.net Pin
buddenn9-Sep-16 5:03
buddenn9-Sep-16 5:03 
AnswerRe: Combine several images to form a single image using silverlight vb.net Pin
Nitesh Kejriwal9-Sep-16 12:22
professionalNitesh Kejriwal9-Sep-16 12:22 
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
Mannava Siva Aditya13-Oct-15 2:35 
AnswerRe: your code is not working Pin
Nitesh Kejriwal29-Oct-15 0:48
professionalNitesh Kejriwal29-Oct-15 0:48 
GeneralError Pin
Durgesh55822-Sep-15 0:53
Durgesh55822-Sep-15 0:53 
Questionerror Pin
Member 1168109011-May-15 1:57
Member 1168109011-May-15 1:57 
AnswerRe: error Pin
Nitesh Kejriwal11-May-15 2:31
professionalNitesh Kejriwal11-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 Kejriwal12-Jun-14 4:17
professionalNitesh Kejriwal12-Jun-14 4:17 
QuestionWhat is List Pin
rajeeshrer15-Apr-14 21:15
rajeeshrer15-Apr-14 21:15 
AnswerRe: What is List Pin
Nitesh Kejriwal16-Apr-14 7:54
professionalNitesh Kejriwal16-Apr-14 7:54 
AnswerRe: What is List Pin
Member 1386584619-Jun-18 18:59
Member 1386584619-Jun-18 18:59 
Questionwhat is imageLocation? Pin
Yasir Babar1-Mar-14 23:58
Yasir Babar1-Mar-14 23:58 
AnswerRe: what is imageLocation? Pin
Nitesh Kejriwal3-Mar-14 4:24
professionalNitesh Kejriwal3-Mar-14 4:24 
GeneralRe: what is imageLocation? Pin
Yasir Babar3-Mar-14 23:26
Yasir Babar3-Mar-14 23:26 
GeneralRe: what is imageLocation? Pin
Nitesh Kejriwal4-Mar-14 3:36
professionalNitesh Kejriwal4-Mar-14 3:36 
GeneralRe: what is imageLocation? Pin
Member 1075131015-Apr-14 9:04
Member 1075131015-Apr-14 9:04 
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 

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.