我怎样才能找到导致异常的原因:IndexOutOfRangeException?

How can i find what make the exception: IndexOutOfRangeException?

我有这个代码:

private void FindPoints()
{
    try
    {
        GraphicsPath gp = new GraphicsPath();
        int x, y, p, j, wdthHght;
        int bytes;
        byte[] rgbValuesWithClouds;
        byte[] rgbValuesWithoutClouds;
        IntPtr ptr;
        Rectangle rect;
        BitmapData bitmap_Data;

        Bitmap bmpWithClouds; //No memory is allocated
        Bitmap bmpWithoutClouds; //No memory is allocated

        gp.AddEllipse(new RectangleF(73, 72, 367, 367));

        using (bmpWithClouds = new Bitmap(mymem))
        {
            rect = new Rectangle(0, 0, bmpWithClouds.Width, bmpWithClouds.Height);
            wdthHght = bmpWithClouds.Width;
            bitmap_Data = bmpWithClouds.LockBits(rect, ImageLockMode.ReadWrite, bmpWithClouds.PixelFormat);
            ptr = bitmap_Data.Scan0;
            bytes = bitmap_Data.Stride * bmpWithClouds.Height;

            rgbValuesWithClouds = new byte[bytes];

            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValuesWithClouds, 0, bytes);

            bmpWithClouds.UnlockBits(bitmap_Data);
        }

        su = System.IO.Directory.GetCurrentDirectory();
        using (bmpWithoutClouds = new Bitmap(su + "\WithoutClouds.bmp")) //24 bit bitmap
        {
            rect = new Rectangle(0, 0, bmpWithoutClouds.Width, bmpWithoutClouds.Height);

            bitmap_Data = bmpWithoutClouds.LockBits(rect, ImageLockMode.ReadWrite, bmpWithoutClouds.PixelFormat);
            ptr = bitmap_Data.Scan0;
            bytes = bitmap_Data.Stride * bmpWithoutClouds.Height;

            rgbValuesWithoutClouds = new byte[bytes];
            System.Runtime.InteropServices.Marshal.Copy(ptr, rgbValuesWithoutClouds, 0, bytes);
            bmpWithoutClouds.UnlockBits(bitmap_Data);
        }
        cloudPoints = new List<Point>();

        for (y = 0; y < wdthHght; y++)
        {
            j = 0;
            for (x = 0; x < wdthHght; x++)
            {
                p = y * wdthHght * 3 + j;
                if (rgbValuesWithClouds[p] != rgbValuesWithoutClouds[p])
                {
                    cloudPoints.Add(new Point(x, y));
                }

                j += 3;
            }
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.ToString());
    }
}

第 393 行出现异常:

if (rgbValuesWithClouds[p] != rgbValuesWithoutClouds[p])

我使用了 try 和 catch,但我看不到 prgbValuesWithClouds 以及 rgbValuesWithoutClouds 的值是什么。

还有什么可以使这个例外?

System.IndexOutOfRangeException was caught HResult=-2146233080 Message=Index was outside the bounds of the array. Source=My Weather Station StackTrace: at mws.ScanningClouds.FindPoints() in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\ScanningClouds.cs:line 393 InnerException:

您已经知道在

行中有一个IndexOutOfRangeException
if (rgbValuesWithClouds[p] != rgbValuesWithoutClouds[p])

这意味着 p 小于 0 或大于 rgbValuesWithCloudsrgbValuesWithoutClouds.

的长度

在进行比较之前,您可能需要检查 p 是否大于或等于 0 并且小于两个数组的长度。