简体   繁体   中英

How can i find what make the exception: IndexOutOfRangeException?

I have this code:

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());
    }
}

The exception is on the line number 393:

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

I used try and catch but i can't see what is the values of p and rgbValuesWithClouds and rgbValuesWithoutClouds are.

And also what can make this exception ?

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:

You already know that you have an IndexOutOfRangeException in the line of

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

which means that p is either smaller than 0 or bigger than the length of rgbValuesWithClouds or rgbValuesWithoutClouds .

You might want to check whether p is greater or equal than 0 and smaller than the length of both of your arrays before you make this comparison.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM