【问题标题】:Combine a lot (~1000-2000) of Jpeg images into one将很多 (~1000-2000) 的 Jpeg 图像合并为一张
【发布时间】: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。

标签: c# graphics bitmap gdi


【解决方案1】:

为什么不使用 xna 或 opengl 之类的东西呢?

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

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

我做了类似的事情来制作精灵表,你基本上使用任何镶嵌或组织算法来创建一个巨大的纹理 2d 以优化空间使用。不过有很多方法可以做到这一点。

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

可能只需要几个小时就可以完成。 XNA v 很容易,特别是如果您只是依靠外部设备。这样做应该会很好。

【讨论】:

    【解决方案2】:

    问题出在这行代码上。

    result.Save("result.jpg", ImageFormat.Jpeg);
    

    看起来它在保存 jpeg/png 格式时会引发错误。我已将您的代码副本加载到 VS2008 + Windows 7 中。

    如果您想使用相同的代码,请将图像格式更改为 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
    

    【讨论】:

      猜你喜欢
      • 2013-07-18
      • 1970-01-01
      • 2017-12-25
      • 2016-03-31
      • 2022-07-01
      • 1970-01-01
      • 2016-09-20
      • 1970-01-01
      相关资源
      最近更新 更多