【问题标题】:WinForms program throwing System.ArgumentException in System.Drawing.dll and I don't know whyWinForms 程序在 System.Drawing.dll 中抛出 System.ArgumentException,我不知道为什么
【发布时间】:2016-12-14 14:31:08
【问题描述】:

我编写了一个小程序,它扫描文件夹中的文件,如果它们是图像文件,程序将检查图像的宽度、高度和 DPI 是否与用户在表单中输入的内容相匹配。

对于与这三个类别中的任何一个不匹配的每个图像,程序将文件名写入文本文件(逗号分隔),并测试图像失败/通过。本质上,它确保文件夹中的所有图像都是用户定义的尺寸和dpi。

这是“扫描”方法的代码:

else
        {
            string[] filesInDirectory = Directory.GetFiles(scanDirTb.Text);

            int i = 0;
            int n = 100;
            string width = widthTb.Text;
            string height = heightTb.Text;
            string dpi = dpiTb.Text;

            foreach (string element in filesInDirectory)
            {
                string ext = Path.GetExtension(filesInDirectory[i]);

                if (i == n)
                {
                    //update the scanned images label every 100 images to show progress
                    scannedImagesLbl.Text = "Images scanned : " + i + "/" + filesInDirectory.Length;
                    n = n + 100;
                    Refresh();
                }

                if (ext.Contains(".jpg") || ext.Contains(".jpeg") || ext.Contains(".gif") || ext.Contains(".bmp") || ext.Contains(".png"))
                {
                    using(Bitmap currentImage = new Bitmap(filesInDirectory[i]))
                    {
                        bool widthMatch  = widthMatches (currentImage, width);
                        bool heightMatch = heightMatches(currentImage, height);
                        bool dpiMatch    = dpiMatches   (currentImage, dpi);

                        if (!widthMatch || !heightMatch || !dpiMatch)
                        {
                            string fileName = Path.GetFileName(filesInDirectory[i]);
                            writeLog(fileName, widthMatch, heightMatch, dpiMatch);
                            i++;
                        }
                        else
                        {
                            i++;
                        }
                    }

                }
                else
                {
                    i++;
                }
            }

            scannedImagesLbl.Text = "Images scanned : " + i + "/" + filesInDirectory.Length;

        }
    }

似乎工作正常 - 写入文件等没有问题。但是,我正在尝试扫描大约 21,000 个文件,其中大约 20,950 个是图像,当我得到大约 11,000 个图像时,程序会抛出这个异常:

System.Drawing.dll 中出现“System.ArgumentException”类型的未处理异常

附加信息:参数无效。

在线:

                    using(Bitmap currentImage = new Bitmap(filesInDirectory[i]))

我之前也遇到过这个问题,但原因是权限问题。这里的权限应该没有问题,所有文件都在一个文件夹中,没有设置特殊权限。

有人知道导致此异常的原因吗?

编辑:认为这可能很重要 - 程序每次都会在大约 11,000 张图像中抛出此异常。我已经测试了 4 次。

【问题讨论】:

  • 异常发生时能否提供filesInDirectory[i]的值?
  • filesInDirectory {string[21281]}, i = 11059。我想我开始意识到我搞砸了
  • 不,没关系。以为可能是因为我在不应该的地方增加了,但是我走了过去,看起来还不错……仍然迷路
  • 你还能提供给我们实际的字符串吗?
  • 显然在那个特定点的特定文件/文件名有问题。您应该查看它以评估问题。

标签: c# winforms exception


【解决方案1】:

问题是一个 0kb 的图像文件......我觉得很愚蠢

【讨论】:

    猜你喜欢
    • 2016-07-28
    • 1970-01-01
    • 1970-01-01
    • 2016-08-06
    • 2020-01-15
    • 2015-11-09
    • 2019-04-12
    • 2017-03-24
    • 1970-01-01
    相关资源
    最近更新 更多