将大量(~1000-2000)的Jpeg图像合并为一个

Tam*_*rga 6 c# graphics gdi bitmap

我现在正在使用以下代码,它适用于~300个图像,但我必须合并超过一千个.

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

它抛出以下异常:

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

Additional information: A generic error occurred in GDI+.
Run Code Online (Sandbox Code Playgroud)

有人可以帮忙吗?

Joh*_*las 1

为什么不使用 xna 或 opengl 之类的东西来做到这一点?

我知道你可以使用texture2d...而且我目前正在学习opengl id喜欢认为你可以使用texture2d,但不知道如何。

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

我做了一些类似于制作精灵表的事情,您基本上使用任何曲面细分或组织算法来创建一个巨大的texture2d来优化空间使用。不过,有很多方法可以做到这一点。

例如 http://xbox.create.msdn.com/en-US/education/catalog/sample/sprite_sheet

可能只需要几个小时就可以完成。XNA v 很简单,尤其是如果您只依赖外部设备的话。这样做应该效果很好。