保存多个图像后 C# GDI+ 一般错误/外部异常

标签 c# image-processing gdi+ tiff

我有一个用 C# 编写的小型控制台应用程序,它以 .jpg 格式从一个文件夹(包含数百张图像)中拍摄一对图片,将它们合并为一张图片,并将结果以 .tif 格式存储在另一个文件夹中。对源文件夹中的所有图片重复此过程。

它在几次循环迭代中工作正常,但随后在尝试保存结果时出现未处理的外部异常和 GDI+ 一般错误(内部异常为空)。当我创建用于存储的新文件并且它在崩溃前适用于一些图片时,我认为这不是由某些权限问题或文件锁定引起的。我怀疑这可能是一些内存问题,因为当我为 .tif 使用 24 位(Format24bppRgb)时,它会在大约 13 次迭代后崩溃,而当我使用 48 位(Format48bppRgb)时,它会在大约 8 次迭代后崩溃。

当它崩溃时要保存的当前图像已经作为一个小于 1KB 的空 .tif 存在于磁盘上。该应用程序不是从 Visual Studio 运行,而是直接从 .exe 运行(因为我读过在从 Visual Studio 运行的应用程序中创建大图像时的内存问题)

源图像的大小范围从 800x600 到 24 兆像素。结果尺寸可以更大,因为当横向图像与纵向图片组合时,会生成具有更大宽度和高度的源图像的方形图像。我运行该应用程序的机器有 8GB 内存,其中最大内存约为 8GB。已使用 50%。

循环看起来像这样:

  foreach (var f in files)
        {
            if ((count % 2) == 0)
            {
                bmp1 = new Bitmap(f);
                int index2 = Array.IndexOf(files, f) + 1;
                if (index2 >= files.Length) { break; }
                bmp2 = new Bitmap(files.ElementAt(index2));
                Merge2Rand(bmp1, bmp2,rand).Save(outputDir + "\\0\\out" + count + ".tif", myImageCodecInfo, myEncoderParameters);
                Console.WriteLine(count + " - " + watch.ElapsedMilliseconds / 60000 + "min");
            }
            count++;
        }

这是合并函数:

 public static Bitmap Merge2Rand(Bitmap bmp1, Bitmap bmp2, Random rand)
    {
        int newH = Math.Max(bmp2.Height, bmp1.Height);
        int newW = Math.Max(bmp2.Width, bmp1.Width);

        int offsetH1 = 0;
        if (bmp1.Height < newH) offsetH1 = rand.Next(0, (newH - bmp1.Height) + 1);
        int offsetW1 = 0;
        if (bmp1.Width < newW) offsetW1 = rand.Next(0, (newW - bmp1.Width) + 1);
        int offsetH2 = 0;
        if (bmp2.Height < newH) offsetH2 = rand.Next(0, (newH - bmp2.Height) + 1);
        int offsetW2 = 0;
        if (bmp2.Width < newW) offsetW2 = rand.Next(0, (newW - bmp2.Width) + 1);

        Bitmap newBMP = new Bitmap(newW, newH, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
        for (int i = 0; i < newW; i++)
        {
            for (int j = 0; j < newH; j++)
            {
                Color p1;
                Color p2;

                if ((i>=offsetW1 && bmp1.Width+offsetW1 > i+offsetW1 ) && (j>=offsetH1 && bmp1.Height+offsetH1 > j+offsetH1))
                {
                    p1 = bmp1.GetPixel(i, j);
                }
                else
                {

                    p1 = Color.FromArgb(0, 0, 0, 0);
                }
                if ((i>=offsetW2 && bmp2.Width + offsetW2 > i +offsetW2) && (j>=offsetH2 && bmp2.Height + offsetH2 > j +offsetH2))
                {
                    p2 = bmp2.GetPixel(i, j);
                }
                else
                {
                    /
                    p2 = Color.FromArgb(0, 0, 0, 0);
                }
                int rVal = (p1.R + p2.R) / 2;
                int gVal = (p1.G + p2.G) / 2;
                int bVal = (p1.B + p2.B) / 2;
                newBMP.SetPixel(i, j, Color.FromArgb(0, rVal, gVal, bVal));
            }
        }
        return newBMP;

我对 .tif 文件使用以下编码:

ImageCodecInfo myImageCodecInfo;
        myImageCodecInfo = GetEncoderInfo("image/tiff");
        System.Drawing.Imaging.Encoder myEncoder;
        myEncoder = System.Drawing.Imaging.Encoder.Compression;
        EncoderParameters myEncoderParameters;
        myEncoderParameters = new EncoderParameters(1);
        EncoderParameter myEncoderParameter;
        myEncoderParameter = new EncoderParameter(myEncoder, (long)EncoderValue.CompressionLZW);
        myEncoderParameters.Param[0] = myEncoderParameter;



private static ImageCodecInfo GetEncoderInfo(String mimeType)
        {
            int j;
            ImageCodecInfo[] encoders;
            encoders = ImageCodecInfo.GetImageEncoders();
            for (j = 0; j < encoders.Length; ++j)
            {
                if (encoders[j].MimeType == mimeType)
                    return encoders[j];
            }
            return null;
        }

最佳答案

问题似乎是您在每次迭代中重复创建 位图并且从不处理它们。这也会导致 GDI 或 USER 对象和内存泄漏。当您的代码使用完对象时,using 语句会自动释放对象。

你需要这样的东西(应该很接近):

foreach (var f in files)
{
    using (Bitmap bmp1 = new Bitmap(f))
    {
        int index2 = Array.IndexOf(files, f) + 1;
        if (index2 >= files.Length) { break; }

        using (Bitmap bmp2 = new Bitmap(files.ElementAt(index2)),
                bmp3 = Merge2Rand(bmp1, bmp2, rand))
        {
            bmp3.Save(outputDir + "\\0\\out" + count + ".tif", myImageCodecInfo, myEncoderParameters);
        }
    }
}

bmp3using block 有点矫枉过正,可以简单地声明它,然后在保存后使用 .Dispose()。它展示了如何在一个 using block 中“堆叠”2 个对象。

如果某物具有 .Dispose() 方法,则意味着当您的代码使用完它时,它应该被丢弃。它意味着您可以在 using block 中使用它以在最后自动处理它。

另见 C# using statement

关于保存多个图像后 C# GDI+ 一般错误/外部异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28321653/

相关文章:

python - 将图像分割成任意数量的框

image-processing - 使用 matplotlib 检测图像中的鼠标事件

windows - 如何重新分配 Gdiplus (GDI+)?

C++ gdi 图像数组

c# - Entity Framework 如何处理数据库表中的默认约束?

C# if else 快捷方式

java - 将 SOAP 响应反序列化到生成的类 C#

c# - Entity Framework Core 原始 SQL 错误 : System. ArgumentException 已添加具有相同键的项目。关键:身份证

python - 准确的二值图像分类

wpf - 将 GDI+ PixelFormat 转换为 WPF PixelFormat