Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


C# Join 2 images to create one

Posted on 2006-05-18
Medium Priority
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
  • 2
  • 2
LVL 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 16714592
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

ID: 16739667
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 86

Expert Comment

by:Mike Tomlinson
ID: 16743575
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

ID: 16747759
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 http://www.byrd48.net/imagetest.htm

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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…

564 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