如何修复控制台应用程序异常“'System.ArgumentException' in System.Drawing.dll”

How to fix Console app exception "'System.ArgumentException' in System.Drawing.dll"

我正在尝试制作一个脚本,将图像列表与一个比较图像进行比较,并写入不同图像的文件名。我把它变成了一个控制台应用程序,因为我不需要显示任何图像。我添加了 System.Drawing 引用,但是当我 运行 我的代码时,我收到消息: ArgumentException: Parameter is not valid.

详情:

System.TypeInitializationException
  HResult=0x80131534
  Message=The type initializer for 'ConsoleApp2.Program' threw an exception.
  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

Inner Exception 1:
ArgumentException: Parameter is not valid.

输出显示:

Exception thrown: 'System.ArgumentException' in System.Drawing.dll An unhandled exception of type 'System.TypeInitializationException' occurred in Unknown Module. The type initializer for 'ConsoleApp2.Program' threw an exception.

我不确定为什么会抛出异常。删除对 System.Drawing 的引用并注释所有使用位图的代码会停止异常,尽管这不是很有用。

这是我运行宁的代码:

static Bitmap comparison = new Bitmap("Comparison.jpg");
static void Main(string[] args)
{
    string[] fileArray = Directory.GetFiles(filepath);

    string compare = AnalyzeBitmap(comparison);

    foreach(string file in fileArray)
    {
        Bitmap hold = null;
        hold = new Bitmap(file);
        AnalyzeBitmap(hold);
        if (hold.Equals(compare))
        {
            Console.WriteLine(file);        
        }
    }

    Console.ReadKey();
}
AnalyzeBitmap():
static string AnalyzeBitmap(Bitmap bmp)
{
    var data = bmp.LockBits(
                           new Rectangle(Point.Empty, comparison.Size),
                           ImageLockMode.ReadWrite, 
                           comparison.PixelFormat
                           );

    var pixelSize = data.PixelFormat == PixelFormat.Format32bppArgb ? 4 : 3;
    var padding = data.Stride - (data.Width * pixelSize);
    var bytes = new byte[data.Height * data.Stride];

    // copy the bytes from bitmap to array
    Marshal.Copy(data.Scan0, bytes, 0, bytes.Length);

    var index = 0;
    var builder = new StringBuilder();

    for (var y = 0; y < data.Height; y++)
    {
        for (var x = 0; x < data.Width; x++)
        {
            Color pixelColor = Color.FromArgb(
                            pixelSize == 3 ? 255 : bytes[index + 3], // A component if present
                            bytes[index + 2], // R component
                            bytes[index + 1], // G component
                            bytes[index]      // B component
                            );

            builder
                   .Append("  ")
                   .Append(pixelColor.R)
                   .Append("     ")
                   .Append(pixelColor.G)
                   .Append("     ")
                   .Append(pixelColor.B)
                   .Append("     ")
                   .Append(pixelColor.A)
                   .AppendLine();



            index += pixelSize;
        }

        index += padding;
    }

    // copy back the bytes from array to the bitmap
    Marshal.Copy(bytes, 0, data.Scan0, bytes.Length);

    return builder.ToString();

}

问题出在这一行

static Bitmap comparison = new Bitmap("Comparison.jpg");

因为 TypeInitializationException 是一个内部异常,只有在静态构造函数或静态字段初始值设定项中发生异常时才会抛出该异常。在您的情况下,没有静态构造函数,只有静态字段初始值设定项。

我认为指定的文件不存在。 请检查您的应用程序的输出路径中是否有名为 Comparison.jpg 的文件。

您还应该将 Bitmap 实例包装在 using 语句中,从而将静态 Bitmap 转换为局部变量。 参见:What are the uses of "using" in C#