【发布时间】:2014-06-02 08:00:28
【问题描述】:
我现在正在使用以下代码,它适用于大约 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);
}
}
它会抛出以下异常:
An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll
Additional information: A generic error occurred in GDI+.
有人可以帮忙吗?
【问题讨论】:
-
我不能,但我确实记得收到了那个错误,因为源图像的格式是 windows/GDI 不喜欢的,我对此无能为力。我能够在 MS paint 中打开它并保存它然后工作,但是 IDK 如何在命令行上使用它。我尝试使用 imagemagick convert 来修复它,但无论我做了什么都没有解决问题,我发布了一个没有答案的问题。
-
很有可能您遇到了 GDI+ 的限制。尝试查看开源图像处理包,例如 aforge.net (www.aforgenet.com/framework/)。如果你只是想创建一个马赛克而不需要自己编码,我已经成功使用andreaplanet.com/andreamosaic/download(免费供个人使用)。
-
但是,如果您不需要在 .NET 中加载图像,那么您可以使用 imagicmagick 或缩略图命令行工具。我想不出任何副手,但我知道我 irfanview 可以将许多图像合并为一个图像,它也许可以在命令行上完成
-
什么时候抛出异常?在开始时还是在几次迭代之后(我假设后者考虑到你所说的)?可能是您正在加载不受支持的格式,或者图形对象中的内存不足。我真的不知道 GDI 是如何处理这个问题的,但是一些断点可能有助于识别问题。
-
附加 342 (192 px) 文件时抛出异常,但不是 341。342 图像的生成位图图像的宽度为 65,664。 341 幅图像的位图宽度为 65,472。 GDI+ 可能使用 unsigned short int 来表示宽度和高度,其最大值为 65,536。