将许多(约1000-2000)Jpeg图像合并为一个图像
本文关键字:图像 一个 合并 Jpeg 许多 1000-2000 | 更新日期: 2023-09-27 17:59:00
我现在正在使用以下代码,它适用于大约300个图像,但我必须合并1000多个图像。
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);
}
}
它抛出以下异常:
An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
Additional information: A generic error occurred in GDI+.
有人能帮忙吗?
为什么不使用xna或opengl之类的东西来实现这一点?
我知道你可以用texture2d。。。由于我目前正在学习opengl,我想你可以用它,但不知道怎么做。
http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.texture2d_members.aspx(在公共上另存为JPEG)
我做了一些类似于制作精灵表的事情,你基本上可以使用任何镶嵌或组织算法来创建一个巨大的texture2d,以优化空间使用。不过,有很多方法可以做到这一点。
例如http://xbox.create.msdn.com/en-US/education/catalog/sample/sprite_sheet
可能只需要几个小时就可以完成。XNA很容易,尤其是如果你只是依靠外在。这样做应该效果很好。
问题在于这行代码。
result.Save("result.jpg", ImageFormat.Jpeg);
看起来它在保存jpeg/png格式时抛出了一个错误。我已将您的代码副本加载到VS2008+Windows7中。
如果您想使用相同的代码,请将图像格式更改为bmp或tiff
result.Save("result.bmp", ImageFormat.Bmp); // this works, but the file size is huge
或
result.Save("result.tiff", ImageFormat.Tiff); // this also works, files is not as big