Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combine a lot (~1000-2000) of Jpeg images into one

I'm using the following code right now, and it works for ~300 images, but I have to merge more than one thousand.

private static void CombineThumbStripImages(string[] imageFiles)
{
    int index = 0;
    using (var result = new Bitmap(192 * imageFiles.Length, 112))
    {
        using (var graphics = Graphics.FromImage(result))
        {
            graphics.Clear(Color.White);
            int leftPosition = 0;
            for (index = 0; index < imageFiles.Length; index++)
            {
                string file = imageFiles[index];
                using (var image = new Bitmap(file))
                {
                    var rect = new Rectangle(leftPosition, 0, 192, 112);
                    graphics.DrawImage(image, rect);
                    leftPosition += 192;
                }
            }
        }
        result.Save("result.jpg", ImageFormat.Jpeg);
    }
}

It throws the following exception:

An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll

Additional information: A generic error occurred in GDI+.

Can somebody help?

like image 962
Tamás Varga Avatar asked Apr 17 '14 21:04

Tamás Varga


People also ask

How do I save multiple images as one PDF?

Hold down the CMD key as you make your selection to choose multiple images, then right-click and select Open with > Preview. Click-and-drag the photos in the sidebar to rearrange their order. When you're satisfied, select File > Print. In the PDF drop-down menu, choose Save as PDF.

How do I make a JPEG file?

Click the “File” menu and then click the “Save As” command. In the Save As window, choose the JPG format on the “Save As Type” drop-down menu and then click the “Save” button.


1 Answers

Why not use something like xna or opengl to do this?

I know you can with texture2d ... and as i am currently learning opengl id like to think you can with that but do not know how.

http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.texture2d_members.aspx (SaveAsJPEG on public)

I have done something similar to make sprite sheets, you basically create a huge texture2d using whatever tesselation or organizing algorithm to optimize space usage. There are lots of ways to do this though.

such as http://xbox.create.msdn.com/en-US/education/catalog/sample/sprite_sheet

would probably only take a couple of hours to do. XNA v easy especially if you are just leaning on the externals. Doing it this was should work pretty well.

like image 186
John Nicholas Avatar answered Oct 11 '22 05:10

John Nicholas