[英]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 and catch,但是我看不到p
和rgbValuesWithClouds
和rgbValuesWithoutClouds
的值是什么。
還有什么可以使此異常?
捕獲了System.IndexOutOfRangeException HResult = -2146233080消息=索引超出了數組的范圍。 來源=我的氣象站StackTrace:位於d:\\ C-Sharp \\ Download File \\ Downloading-File-Project-Version-012 \\ Downloading File \\ ScanningClouds.cs:line 393中的mws.ScanningClouds.FindPoints()InnerException:
您已經知道在以下行中有一個IndexOutOfRangeException
if (rgbValuesWithClouds[p] != rgbValuesWithoutClouds[p])
這意味着p
小於0
或大於rgbValuesWithClouds
或rgbValuesWithoutClouds
的長度。
在進行此比較之前,您可能想檢查p
是否大於或等於0
且小於兩個數組的長度。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.