locked
How to combine multiple jpg into one jpg image RRS feed

  • Question

  • User869176912 posted

    Hi,

    I have a folder that contains multiple jpg images(amount of image may vary). I want to combine any images with similar filename in folder into one jpg image. 

    i.e.

    c:\ImageFolder\

    contains: image1_1.jpg, image1_2.jpg, image2_1.jpg, image2_2.jpg, image2_3.jpg, etc.

    So I want to make one combined image of image1 and 1 combined image of image2, etc.

    How can I do this?

    Saturday, May 12, 2018 9:07 PM

Answers

  • User-330142929 posted

    I have a folder that contains multiple jpg images(amount of image may vary). I want to combine any images with similar filename in folder into one jpg image. 

    Hi MikeT89.

    According to your description, I suggest you could use Graphics.DrawImage method to achieve your requirements.

    I suggest you could filter the image according to the image path, then you could generate the image according to all the images in the same category.

    Notice:

    I generated the image with the highest image height and the sum of all each images width. You cold modify my demo codes according to your requirement.

    Gif Demo.

    Code behind.

    protected void Button1_Click(object sender, EventArgs e)
            {
                DirectoryInfo di = new DirectoryInfo(@"D:\Images");
                //Get all classifications
                var categories = di.GetFiles().Select(p => p.Name.Split('_')[0]).Distinct();
                //one classification generates one picture
                foreach (string category in categories)
                {
                    List<string> filelist = new List<string>();
                    int width = 0;
                    int height = 0;
                    int Imgwidth = 0;
                    foreach (var item in di.GetFiles())
                    {
                        if (item.Name.Split('_')[0]==category)
                        {
                            filelist.Add(item.FullName);
                            System.Drawing.Image img = System.Drawing.Image.FromFile(item.FullName);
                            width += img.Width;
                            if (img.Height>height)
                            {
                                height = img.Height;
                            }
                        }
                    }
                    Bitmap bmp = new Bitmap(width, height);
                    Graphics g = Graphics.FromImage(bmp);
                    foreach (var item2 in filelist)
                    {
                        System.Drawing.Image img1 = System.Drawing.Image.FromFile(item2.ToString());
                        g.DrawImage(img1, new Rectangle(Imgwidth, 0, img1.Width, img1.Height));
                        Imgwidth += img1.Width;
                    }
                    bmp.Save(@"D:\Images\" + category + ".jpg", ImageFormat.Jpeg);
                }                       
            }

    If the solution could not solve your problem. Please feel free to let me know.

    Best regards
    Abraham

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, May 14, 2018 10:20 AM