C# Join 2 images to create one

Posted on 2006-05-18
Last Modified: 2008-01-16
I would like to create a single jpeg file by taking two existing jpegs, placing them side by side and 'gluing them together' to create one and save it to disk.  Please provide a code snippet on how to accomplish this in C# or VB.NET.
Question by:spmcmorrow
    LVL 85

    Accepted Solution

    Something like...

            private void button1_Click(object sender, EventArgs e)
                String jpg1 = @"c:\someFileA.jpg";
                String jpg2 = @"c:\someFileB.jpg";
                String jpg3 = @"c:\someFileC.jpg";

                Image img1 = Image.FromFile(jpg1);
                Image img2 = Image.FromFile(jpg2);

                int width = img1.Width + img2.Width;
                int height = Math.Max(img1.Height, img2.Height);

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

                g.DrawImage(img1, new Point(0, 0));
                g.DrawImage(img2, new Point(img1.Width, 0));


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

    Author Comment

    Hi and thanks.
    I have another question, when determining the placement of the images (new point()) is this measurement in pixels?  Using the example you gave above, Both images are 48 pixels wide by 67 pixels tall.  Let's say I wanted to place them in two rows of two columns.  When I place img1 at (0,0) and img2 at (49,0), they line up fine.  But when I add img3 at (0,68) and img4 at (49,68), the bottom ones overlap the top ones.  Does this make sense?
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    It should be measured in Pixels.


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

                g.DrawImage(img1, new Point(0, 0));
                g.DrawImage(img2, new Point(img1.Width, 0));
                g.DrawImage(img1, new Point(0, img1.Height));
                g.DrawImage(img2, new Point(img1.Width, img1.Height));


    Author Comment

    I'm still having issues.  I can get the images to add, but they are overlapping.  In this test example, I'm using one image, SlotLarge.jpg which is 48 x 67 pixels.
    In the code below, you'll see that I add the first one at (0, 0) and the second one at (0, 68).  The second one is overlapping half of the first one.  I posted the original image and compiled image at

    Here is the code and thanks again for your help:

            m_fileDir = @"c:\inetpub\wwwroot\pfs\images\machines\45countlarge\";

            m_Bitmap = new Bitmap(320, 600);
            Graphics g = Graphics.FromImage(m_Bitmap);

            System.Drawing.Image _img = System.Drawing.Image.FromFile(m_fileDir + "SlotLarge.jpg");
            g.DrawImage(_img, 0, 0);

            g.DrawImage(_img, 0, 68);

            m_Bitmap.Save(m_fileDir + "testimage.jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    Help with my python script 6 137
    hasOne  challenge 59 66
    copyEndy  challenge 15 41
    for loop with Set 4 38
    Purpose To explain how to place a textual stamp on a PDF document.  This is commonly referred to as an annotation, or possibly a watermark, but a watermark is generally different in that it is somewhat translucent.  Watermark’s may be text or graph…
    In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now